package com.erp.tenant.dto;

import com.erp.tenant.entity.Package;
import com.erp.tenant.entity.Tenant;
import com.erp.tenant.entity.TenantStats;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

/**
 * 租户详情响应DTO
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TenantDetailResponse {
    
    private Long id;
    private String name;
    private String code;
    private String domain;
    private Integer status;
    private String statusLabel;
    private PackageInfo packageInfo;
    private String adminEmail;
    private String adminPhone;
    private LocalDateTime expireTime;
    private Boolean isExpired;
    private Boolean isExpiringSoon;
    private String remark;
    private String settings;
    private TenantStats stats;
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;
    
    /**
     * 套餐信息
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class PackageInfo {
        private Long id;
        private String name;
        private String code;
        private String type;
        private String typeLabel;
    }
    
    /**
     * 从实体转换为响应DTO
     */
    public static TenantDetailResponse fromEntity(Tenant tenant, TenantStats stats) {
        if (tenant == null) {
            return null;
        }
        
        TenantDetailResponseBuilder builder = TenantDetailResponse.builder()
                .id(tenant.getId())
                .name(tenant.getName())
                .code(tenant.getCode())
                .domain(tenant.getDomain())
                .status(tenant.getStatus())
                .statusLabel(tenant.getStatusLabel())
                .adminEmail(tenant.getAdminEmail())
                .adminPhone(tenant.getAdminPhone())
                .expireTime(tenant.getExpireTime())
                .isExpired(tenant.isExpired())
                .isExpiringSoon(tenant.isExpiringSoon())
                .remark(tenant.getRemark())
                .settings(tenant.getSettings())
                .stats(stats)
                .createdAt(tenant.getCreatedAt())
                .updatedAt(tenant.getUpdatedAt());
        
        // 设置套餐信息
        if (tenant.getPkg() != null) {
            Package pkg = tenant.getPkg();
            builder.packageInfo(PackageInfo.builder()
                    .id(pkg.getId())
                    .name(pkg.getName())
                    .code(pkg.getCode())
                    .type(pkg.getType())
                    .typeLabel(pkg.getTypeLabel())
                    .build());
        }
        
        return builder.build();
    }
}
