using Microsoft.EntityFrameworkCore;
using SolutionErp.Domain.ApprovalWorkflowsV2;
using SolutionErp.Domain.ContractSigningPlans;
using SolutionErp.Domain.Contracts;
using SolutionErp.Domain.Contracts.Details;
using SolutionErp.Domain.Forms;
using SolutionErp.Domain.Hrm;
using SolutionErp.Domain.Identity;
using SolutionErp.Domain.Master;
using SolutionErp.Domain.Master.Catalogs;
using SolutionErp.Domain.Notifications;
using SolutionErp.Domain.Office;
using SolutionErp.Domain.PurchaseEvaluations;

namespace SolutionErp.Application.Common.Interfaces;

public interface IApplicationDbContext
{
    DbSet<Supplier> Suppliers { get; }
    DbSet<Project> Projects { get; }
    DbSet<Department> Departments { get; }
    // 4 master catalogs cho Details add form (autocomplete)
    DbSet<UnitOfMeasure> UnitsOfMeasure { get; }
    DbSet<MaterialItem> MaterialItems { get; }
    DbSet<ServiceItem> ServiceItems { get; }
    DbSet<WorkItem> WorkItems { get; }
    // [K1 S164 — Mig 70] Danh mục Hợp đồng SOL-CCM-SP-002 (86 dòng × 8 nhóm duyệt)
    DbSet<ContractCatalogEntry> ContractCatalogEntries { get; }
    DbSet<MenuItem> MenuItems { get; }
    DbSet<Permission> Permissions { get; }
    DbSet<User> Users { get; }
    DbSet<Role> Roles { get; }
    DbSet<ContractTemplate> ContractTemplates { get; }
    DbSet<ContractClause> ContractClauses { get; }
    DbSet<Contract> Contracts { get; }
    DbSet<ContractApproval> ContractApprovals { get; }
    DbSet<ContractComment> ContractComments { get; }
    DbSet<ContractAttachment> ContractAttachments { get; }
    DbSet<ContractCodeSequence> ContractCodeSequences { get; }
    DbSet<ContractChangelog> ContractChangelogs { get; }
    DbSet<ContractDepartmentApproval> ContractDepartmentApprovals { get; }
    // Plan B Chunk C (S29 — Mig 33) — Ý kiến cấp duyệt V2 dynamic cho HĐ.
    // Cookie-cutter mirror PE Mig 26 PurchaseEvaluationLevelOpinions.
    DbSet<ContractLevelOpinion> ContractLevelOpinions { get; }
    DbSet<Notification> Notifications { get; }
    DbSet<WorkflowTypeAssignment> WorkflowTypeAssignments { get; }
    DbSet<WorkflowDefinition> WorkflowDefinitions { get; }
    DbSet<WorkflowStep> WorkflowSteps { get; }
    DbSet<WorkflowStepApprover> WorkflowStepApprovers { get; }

    // Per-type Details DbSets (Option B — bảng riêng từng loại HĐ)
    DbSet<ThauPhuDetail> ThauPhuDetails { get; }
    DbSet<GiaoKhoanDetail> GiaoKhoanDetails { get; }
    DbSet<NhaCungCapDetail> NhaCungCapDetails { get; }
    DbSet<DichVuDetail> DichVuDetails { get; }
    DbSet<MuaBanDetail> MuaBanDetails { get; }
    DbSet<NguyenTacNccDetail> NguyenTacNccDetails { get; }
    DbSet<NguyenTacDvDetail> NguyenTacDvDetails { get; }

    // Module Duyệt NCC (tiền-HĐ) — 7 core + 3 workflow config
    DbSet<PurchaseEvaluation> PurchaseEvaluations { get; }
    DbSet<PurchaseEvaluationSupplier> PurchaseEvaluationSuppliers { get; }
    DbSet<PurchaseEvaluationDetail> PurchaseEvaluationDetails { get; }
    DbSet<PurchaseEvaluationQuote> PurchaseEvaluationQuotes { get; }
    DbSet<PurchaseEvaluationApproval> PurchaseEvaluationApprovals { get; }
    DbSet<PurchaseEvaluationChangelog> PurchaseEvaluationChangelogs { get; }
    DbSet<PurchaseEvaluationAttachment> PurchaseEvaluationAttachments { get; }
    DbSet<PurchaseEvaluationWorkflowDefinition> PurchaseEvaluationWorkflowDefinitions { get; }
    DbSet<PurchaseEvaluationWorkflowStep> PurchaseEvaluationWorkflowSteps { get; }
    DbSet<PurchaseEvaluationWorkflowStepApprover> PurchaseEvaluationWorkflowStepApprovers { get; }
    DbSet<PurchaseEvaluationCodeSequence> PurchaseEvaluationCodeSequences { get; }
    DbSet<PurchaseEvaluationDepartmentOpinion> PurchaseEvaluationDepartmentOpinions { get; }
    DbSet<PurchaseEvaluationDepartmentApproval> PurchaseEvaluationDepartmentApprovals { get; }
    // Mig 26 (Session 19) — Ý kiến cấp duyệt V2 dynamic theo ApprovalWorkflowLevel
    DbSet<PurchaseEvaluationLevelOpinion> PurchaseEvaluationLevelOpinions { get; }
    // [S61 Mig 50] Ngân sách gói thầu per cặp (ProjectId, WorkItemId) — 1 record/cặp,
    // loose-Guid KHÔNG FK vật lý. Thay module Budget cũ (5 bảng đã drop).
    DbSet<PeWorkItemBudget> PeWorkItemBudgets { get; }

    // Quy trình duyệt MỚI (Mig 22 — Session 17): schema riêng UAT trước khi
    // drop legacy WorkflowDefinition. Cấu trúc: Quy trình > Bước (Phòng) > Cấp (NV cụ thể).
    DbSet<ApprovalWorkflow> ApprovalWorkflows { get; }
    DbSet<ApprovalWorkflowStep> ApprovalWorkflowSteps { get; }
    DbSet<ApprovalWorkflowLevel> ApprovalWorkflowLevels { get; }

    // Phase 10.1 G-H1 (Mig 34 — S33) — Hồ sơ Nhân sự port từ NamGroup.
    // 1 main + 5 satellite + 1 sequence. 1-1 với User qua UserId UNIQUE.
    // 3 HĐLĐ satellite defer Plan H2 sau.
    DbSet<EmployeeProfile> EmployeeProfiles { get; }
    DbSet<EmployeeWorkHistory> EmployeeWorkHistories { get; }
    DbSet<EmployeeEducation> EmployeeEducations { get; }
    DbSet<EmployeeFamilyRelation> EmployeeFamilyRelations { get; }
    DbSet<EmployeeSkill> EmployeeSkills { get; }
    DbSet<EmployeeDocument> EmployeeDocuments { get; }
    DbSet<EmployeeCodeSequence> EmployeeCodeSequences { get; }

    // Phase 10.2 G-H2 (Mig 35 — S34) — Cấu hình HRM 4 catalog lookup.
    // LeaveTypes + Holidays + ShiftPatterns + OtPolicies. Reference G-O4
    // LeaveRequest workflow + G-P1 chấm công OT calc business logic.
    DbSet<LeaveType> LeaveTypes { get; }
    DbSet<Holiday> Holidays { get; }
    DbSet<ShiftPattern> ShiftPatterns { get; }
    DbSet<OtPolicy> OtPolicies { get; }

    // Phase 11 P11-C (Mig 44 — S51) — Danh mục xe công + tài xế reference VehicleBooking.
    DbSet<Vehicle> Vehicles { get; }
    DbSet<Driver> Drivers { get; }

    // Phase 10.2 G-O2 (Mig 36 — S36) — Phòng họp + Booking + Attendee join.
    // Overlap check qua SERIALIZABLE tx + EXISTS handler (clean-room SOL,
    // mirror NamGroup TblBookingResource S50 pattern). FullCalendar v6 MIT FE.
    DbSet<MeetingRoom> MeetingRooms { get; }
    DbSet<MeetingBooking> MeetingBookings { get; }
    DbSet<MeetingBookingAttendee> MeetingBookingAttendees { get; }

    // Phase 10.3 G-O3 (Mig 38 — S37) — Đề xuất (Proposal) cookie-cutter mirror PE.
    // Workflow V2 dynamic ApplicableType=ProposalGeneral=4 (Mig 37 enum extend).
    // CodeGen "DX/YYYY/NNN" atomic via ProposalCodeSequences.
    DbSet<Proposal> Proposals { get; }
    DbSet<ProposalAttachment> ProposalAttachments { get; }
    DbSet<ProposalLevelOpinion> ProposalLevelOpinions { get; }
    DbSet<ProposalCodeSequence> ProposalCodeSequences { get; }

    // Phase 10.3 G-O4+G-O5+G-O6 (Mig 39 — S38) — Workflow Apps skeleton.
    // 5 entity (Leave/OT/Travel/VehicleBooking/ItTicket) status flat 5-state.
    // ApproveV2 + LevelOpinions per-module DEFER Phase 11.
    DbSet<LeaveRequest> LeaveRequests { get; }
    DbSet<OtRequest> OtRequests { get; }
    DbSet<TravelRequest> TravelRequests { get; }
    DbSet<VehicleBooking> VehicleBookings { get; }
    DbSet<ItTicket> ItTickets { get; }

    // Phase 11 P11-A (Mig 41) — Wire ApproveV2 + LevelOpinions cho 4 WorkflowApps
    // module (cookie-cutter mirror Proposal Mig 38). + shared atomic codegen MaDonTu.
    DbSet<LeaveRequestLevelOpinion> LeaveRequestLevelOpinions { get; }
    DbSet<OtRequestLevelOpinion> OtRequestLevelOpinions { get; }
    DbSet<TravelRequestLevelOpinion> TravelRequestLevelOpinions { get; }
    DbSet<VehicleBookingLevelOpinion> VehicleBookingLevelOpinions { get; }
    DbSet<WorkflowAppCodeSequence> WorkflowAppCodeSequences { get; }

    // Phase 10.4 G-P1 (Mig 40 — S38) — Chấm công web GPS.
    DbSet<Attendance> Attendances { get; }

    // Phase 11 P11-B Wave 1 (Mig 42) — Số dư phép theo năm (NV × LoạiPhép × Năm).
    DbSet<LeaveBalance> LeaveBalances { get; }

    // [W1 Mig 69 — S161] GĐ2 "Kế hoạch ký kết HĐ" (ContractSigningPlan) — 7 bảng.
    // Workflow V2 ApplicableType = ContractSigningPlan = 10 (`ApprovalWorkflow.cs:66`).
    // Mã phiếu "KHKK/{YYYY}/{Seq:D3}" atomic qua `WorkflowAppCodeSequences` DÙNG CHUNG (:136),
    // KHÔNG bảng sequence riêng.
    DbSet<ContractSigningPlan> ContractSigningPlans { get; }
    DbSet<ContractSigningPlanLine> ContractSigningPlanLines { get; }
    DbSet<ContractSigningPlanDossierItem> ContractSigningPlanDossierItems { get; }
    DbSet<ContractSigningPlanLevelOpinion> ContractSigningPlanLevelOpinions { get; }
    DbSet<ContractSigningPlanAttachment> ContractSigningPlanAttachments { get; }
    DbSet<ContractSigningPlanChangelog> ContractSigningPlanChangelogs { get; }
    DbSet<ContractSigningPlanApproval> ContractSigningPlanApprovals { get; }

    Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
}
