package kr.co.accura.wbx.spring.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Data
@Component
@ConfigurationProperties(prefix = "wbx.spring")
public class WbxSpringProperties {

    private String apiPrefix = "/api";
    private String apiVersion = "v1";

    private Jwt jwt = new Jwt();
    private Mfa mfa = new Mfa();
    private Password password = new Password();
    private Cors cors = new Cors();
    private FileConfig file = new FileConfig();
    private Approval approval = new Approval();
    private Notification notification = new Notification();
    private DataSourceConfig datasource = new DataSourceConfig();
    private AdminUi adminUi = new AdminUi();
    private Compat compat = new Compat();

    @Data
    public static class Jwt {
        private String secret = "wbx-spring-default-secret-key-change-in-production-minimum-256-bits";
        private long expiration = 28800; // 8 hours
        private long refreshExpiration = 604800; // 7 days
    }

    @Data
    public static class Mfa {
        private boolean enabled = false;
        private boolean forceForExternal = true;
        private boolean forceForInternal = false;
        private String totpIssuer = "WBX Platform";
        private int totpDigits = 6;
        private int totpPeriod = 30;
    }

    @Data
    public static class Password {
        private int minLength = 8;
        private int maxFailedAttempts = 5;
        private int expiryDays = 90;
        private int historyCount = 3;
        private boolean requireUppercase = true;
        private boolean requireDigit = true;
        private boolean requireSpecial = true;
    }

    @Data
    public static class Cors {
        private List<String> allowedOrigins = new ArrayList<>(List.of("http://localhost:5173", "http://localhost:3000"));
    }

    @Data
    public static class FileConfig {
        private String storageType = "local"; // local, azure-blob, aws-s3, gcp-storage
        private String uploadDir = "./uploads";
        private long maxSizeMb = 50;
        private AzureConfig azure = new AzureConfig();
        private AwsConfig aws = new AwsConfig();
        private GcpConfig gcp = new GcpConfig();

        @Data
        public static class AzureConfig {
            private String accountName = "";
            private String accountKey = "";
            private String containerName = "uploads";
        }

        @Data
        public static class AwsConfig {
            private String bucket = "";
            private String region = "ap-northeast-2";
            private String accessKey = "";
            private String secretKey = "";
        }

        @Data
        public static class GcpConfig {
            private String bucket = "";
            private String projectId = "";
        }
    }

    @Data
    public static class Notification {
        private boolean sseEnabled = true;
        private boolean websocketEnabled = false;
        private int heartbeatSeconds = 30;
    }

    @Data
    public static class AdminUi {
        private boolean enabled = false;
        private String path = "/admin";
        private List<String> allowRoles = new ArrayList<>(List.of("SA"));
    }

    @Data
    public static class Approval {
        private boolean enabled = true;
    }

    @Data
    public static class DataSourceConfig {
        private boolean routingEnabled = false;
    }

    @Data
    public static class Compat {
        private String errorFormat = "fastapi"; // detail key
        private String listKey = "items";
    }

}
