package com.erp.supplier.dto.response;

import com.erp.supplier.entity.Supplier;
import com.erp.supplier.entity.SupplierContact;
import com.erp.supplier.entity.SupplierBankAccount;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.Builder;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Data
@Builder
public class SupplierResponse {

    private Long id;
    private String supplierCode;
    private String supplierName;
    private String supplierType;
    private String supplierTypeText;
    private String industry;
    private String businessLicense;
    private BigDecimal registeredCapital;
    private String establishmentDate;
    private String legalPerson;
    private String phone;
    private String email;
    private String website;
    private String province;
    private String city;
    private String district;
    private String address;
    private String mainBusiness;
    private String status;
    private String statusText;
    private String grade;
    private String gradeText;
    private Integer creditScore;
    private Integer isInternal;
    private String isInternalText;
    private String cooperationStartDate;
    private String cooperationEndDate;
    private String settlementMethod;
    private Integer paymentDays;
    private BigDecimal minimumOrderQuantity;
    private String remark;
    private String attachments;
    private String auditStatus;
    private String auditStatusText;
    private String auditComment;
    private Long auditorId;
    
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime auditTime;
    
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdAt;
    
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updatedAt;

    private List<SupplierContactResponse> contacts;
    private List<SupplierBankAccountResponse> bankAccounts;

    public static SupplierResponse fromEntity(Supplier supplier) {
        return SupplierResponse.builder()
                .id(supplier.getId())
                .supplierCode(supplier.getSupplierCode())
                .supplierName(supplier.getSupplierName())
                .supplierType(supplier.getSupplierType())
                .supplierTypeText(getSupplierTypeText(supplier.getSupplierType()))
                .industry(supplier.getIndustry())
                .businessLicense(supplier.getBusinessLicense())
                .registeredCapital(supplier.getRegisteredCapital())
                .establishmentDate(supplier.getEstablishmentDate())
                .legalPerson(supplier.getLegalPerson())
                .phone(supplier.getPhone())
                .email(supplier.getEmail())
                .website(supplier.getWebsite())
                .province(supplier.getProvince())
                .city(supplier.getCity())
                .district(supplier.getDistrict())
                .address(supplier.getAddress())
                .mainBusiness(supplier.getMainBusiness())
                .status(supplier.getStatus())
                .statusText(getStatusText(supplier.getStatus()))
                .grade(supplier.getGrade())
                .gradeText(getGradeText(supplier.getGrade()))
                .creditScore(supplier.getCreditScore())
                .isInternal(supplier.getIsInternal())
                .isInternalText(supplier.getIsInternal() != null && supplier.getIsInternal() == 1 ? "是" : "否")
                .cooperationStartDate(supplier.getCooperationStartDate())
                .cooperationEndDate(supplier.getCooperationEndDate())
                .settlementMethod(supplier.getSettlementMethod())
                .paymentDays(supplier.getPaymentDays())
                .minimumOrderQuantity(supplier.getMinimumOrderQuantity())
                .remark(supplier.getRemark())
                .attachments(supplier.getAttachments())
                .auditStatus(supplier.getAuditStatus())
                .auditStatusText(getAuditStatusText(supplier.getAuditStatus()))
                .auditComment(supplier.getAuditComment())
                .auditorId(supplier.getAuditorId())
                .auditTime(supplier.getAuditTime())
                .createdAt(supplier.getCreatedAt())
                .updatedAt(supplier.getUpdatedAt())
                .build();
    }

    public static String getSupplierTypeText(String type) {
        if (type == null) return "";
        switch (type) {
            case "manufacturer": return "生产厂商";
            case "trader": return "贸易商";
            case "agent": return "代理商";
            case "individual": return "个体户";
            default: return type;
        }
    }

    public static String getStatusText(String status) {
        if (status == null) return "";
        switch (status) {
            case "pending": return "待合作";
            case "active": return "合作中";
            case "inactive": return "已停用";
            case "blacklist": return "黑名单";
            default: return status;
        }
    }

    public static String getGradeText(String grade) {
        if (grade == null) return "";
        switch (grade) {
            case "A": return "A级(优秀)";
            case "B": return "B级(良好)";
            case "C": return "C级(一般)";
            case "D": return "D级(较差)";
            default: return grade;
        }
    }

    public static String getAuditStatusText(String status) {
        if (status == null) return "";
        switch (status) {
            case "pending": return "待审核";
            case "approved": return "已通过";
            case "rejected": return "已驳回";
            default: return status;
        }
    }
}
