using SolutionErp.Domain.Contracts;

namespace SolutionErp.Application.Contracts.Dtos;

public record ContractListItemDto(
    Guid Id,
    string? MaHopDong,
    string? TenHopDong,
    ContractType Type,
    ContractPhase Phase,
    Guid SupplierId,
    string SupplierName,
    Guid ProjectId,
    string ProjectName,
    decimal GiaTri,
    DateTime? SlaDeadline,
    DateTime CreatedAt);

public record ContractDetailDto(
    Guid Id,
    string? MaHopDong,
    string? TenHopDong,
    string? NoiDung,
    ContractType Type,
    ContractPhase Phase,
    Guid SupplierId,
    string SupplierName,
    Guid ProjectId,
    string ProjectName,
    Guid? DepartmentId,
    string? DepartmentName,
    Guid? DrafterUserId,
    string? DrafterName,
    Guid? TemplateId,
    decimal GiaTri,
    bool BypassProcurementAndCCM,
    DateTime? SlaDeadline,
    string? DraftData,
    DateTime CreatedAt,
    DateTime? UpdatedAt,
    // [S61 Mig 50] Module Budget cũ XÓA — Contract.BudgetId drop, GIỮ BudgetManual*
    // (ngân sách nhập tay của HĐ không đổi, anh Kiệt chỉ redesign ngân sách PE).
    string? BudgetManualName,
    decimal? BudgetManualAmount,
    List<ContractApprovalDto> Approvals,
    List<ContractCommentDto> Comments,
    List<ContractAttachmentDto> Attachments,
    WorkflowSummaryDto Workflow,
    // [Plan B S29 2026-05-22 Chunk E2] V2 workflow fields — mirror PE pattern.
    // ApprovalWorkflowId pin lúc create (Mig 32). FE Section 5 detect V2 mode
    // qua field này: nếu Guid → render dynamic LevelOpinionsSectionV2;
    // nếu null → V1 legacy KHÔNG Section 5 V2.
    // FE fetch ApprovalFlow shape via /api/approval-workflows-v2/{ApprovalWorkflowId}.
    Guid? ApprovalWorkflowId = null,
    int? CurrentApprovalLevelOrder = null,
    List<ContractLevelOpinionDto>? LevelOpinions = null);

// [Plan B S29 2026-05-22 Chunk E2] Ý kiến cấp duyệt V2 dynamic theo
// ApprovalWorkflowLevel. Mirror PE PurchaseEvaluationLevelOpinionDto pattern.
// Service ApproveV2Async UPSERT (Plan B Chunk B2 1f199b0). Comment empty
// fallback "(duyệt — không ý kiến)". SignedByUserId !== Level.ApproverUserId
// → FE show banner "Admin duyệt thay".
public record ContractLevelOpinionDto(
    Guid Id,
    Guid ApprovalWorkflowLevelId,
    int StepOrder,
    string StepName,
    int LevelOrder,
    string? LevelName,
    Guid ApproverUserId,
    string? ApproverFullName,
    string Comment,
    DateTime SignedAt,
    Guid SignedByUserId,
    string? SignedByFullName);

// Policy snapshot for the FE — lets UI render next-phase buttons dynamically
// without hardcoding the transition map (single source of truth in BE).
public record WorkflowSummaryDto(
    string PolicyName,
    string PolicyDescription,
    List<ContractPhase> ActivePhases,
    List<ContractPhase> NextPhases);

public record ContractApprovalDto(
    Guid Id,
    ContractPhase FromPhase,
    ContractPhase ToPhase,
    Guid? ApproverUserId,
    string? ApproverName,
    ApprovalDecision Decision,
    string? Comment,
    DateTime ApprovedAt);

public record ContractCommentDto(
    Guid Id,
    Guid UserId,
    string UserName,
    ContractPhase Phase,
    string Content,
    DateTime CreatedAt);

public record ContractAttachmentDto(
    Guid Id,
    string FileName,
    string StoragePath,
    long FileSize,
    string ContentType,
    AttachmentPurpose Purpose,
    string? Note,
    DateTime CreatedAt);
