package com.erp.tenant.dto;

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

import java.time.LocalDateTime;

/**
 * 租户列表项响应DTO
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TenantListResponse {
    
    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 LocalDateTime createdAt;
    
    /**
     * 套餐信息
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class PackageInfo {
        private Long id;
        private String name;
        private String code;
    }
    
    /**
     * 从实体转换为列表响应DTO
     */
    public static TenantListResponse fromEntity(Tenant tenant) {
        if (tenant == null) {
            return null;
        }
        
        TenantListResponseBuilder builder = TenantListResponse.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())
                .createdAt(tenant.getCreatedAt());
        
        // 设置套餐信息
        if (tenant.getPkg() != null) {
            Package pkg = tenant.getPkg();
            builder.packageInfo(PackageInfo.builder()
                    .id(pkg.getId())
                    .name(pkg.getName())
                    .code(pkg.getCode())
                    .build());
        }
        
        return builder.build();
    }
}
