namespace PowderCoating.Core.Entities;

/// <summary>
/// Company-specific application and workflow preferences
/// </summary>
public class CompanyPreferences : BaseEntity
{
    public int CompanyId { get; set; }

    // Application Defaults
    public string DefaultCurrency { get; set; } = "USD";
    public string DefaultDateFormat { get; set; } = "MM/dd/yyyy";
    public string DefaultTimeFormat { get; set; } = "12h";
    public string DefaultPaymentTerms { get; set; } = "Net 30";
    public int DefaultQuoteValidityDays { get; set; } = 30;
    public string QuoteNumberPrefix { get; set; } = "QT";
    public string JobNumberPrefix { get; set; } = "JOB";
    public string InvoiceNumberPrefix { get; set; } = "INV";
    public bool UseMetricSystem { get; set; } = false; // False = Imperial (ft, lb), True = Metric (m, kg)

    // Default GL Accounts — used as the fallback when an item leaves its account field blank.
    // Null means "no default": revenue falls back to account 4000, and inventory-consumption
    // COGS simply isn't posted (consistent with expensing materials at purchase). A company
    // only opts into perpetual-inventory COGS posting by setting both the COGS and Inventory
    // defaults (or the per-item accounts). FKs are nullable with no cascade — accounts soft-delete.
    /// <summary>Default Revenue account for invoice lines that don't specify one (fallback before account 4000).</summary>
    public int? DefaultRevenueAccountId { get; set; }
    /// <summary>Default COGS account pre-filled on new inventory/catalog items. Drives inventory-consumption COGS posting when paired with an inventory account.</summary>
    public int? DefaultCogsAccountId { get; set; }
    /// <summary>Default Inventory asset account pre-filled on new inventory items. Drives inventory-consumption COGS posting when paired with a COGS account.</summary>
    public int? DefaultInventoryAccountId { get; set; }

    // Job / Workflow Defaults
    public string DefaultJobPriority { get; set; } = "Normal";
    public bool RequireCustomerPO { get; set; } = false;
    public bool AllowCustomerApproval { get; set; } = true;
    public int DefaultTurnaroundDays { get; set; } = 7;
    // Email Sender Identity
    /// <summary>From address used in outgoing emails. Falls back to SendGrid:FromEmail in appsettings when null.</summary>
    public string? EmailFromAddress { get; set; }
    /// <summary>From display name used in outgoing emails. Falls back to SendGrid:FromName in appsettings when null.</summary>
    public string? EmailFromName { get; set; }

    // Notifications & Alerts
    public bool EmailNotificationsEnabled { get; set; } = true;
    public bool NotifyOnNewJob { get; set; } = true;
    public bool NotifyOnNewQuote { get; set; } = true;
    public bool NotifyOnJobStatusChange { get; set; } = true;
    public bool NotifyOnQuoteApproval { get; set; } = true;
    public bool NotifyOnPaymentReceived { get; set; } = true;
    public int QuoteExpiryWarningDays { get; set; } = 3;
    public int DueDateWarningDays { get; set; } = 2;
    public int MaintenanceAlertDays { get; set; } = 7;

    // Payment Reminders
    /// <summary>When true, the background service will send overdue payment reminder emails.</summary>
    public bool PaymentRemindersEnabled { get; set; } = false;
    /// <summary>Comma-separated days-past-due thresholds at which reminders are sent (e.g. "7,14,30").</summary>
    public string PaymentReminderDays { get; set; } = "7,14,30";

    // Data Retention
    public int QuoteRetentionYears { get; set; } = 7;
    public int JobRetentionYears { get; set; } = 7;
    public int LogRetentionDays { get; set; } = 90;
    public int AutoArchiveJobsDays { get; set; } = 365;
    public int DeletedRecordRetentionDays { get; set; } = 30;

    // Quote PDF Template
    public string QtAccentColor { get; set; } = "#374151";
    public string? QtDefaultTerms { get; set; }
    public string? QtFooterNote { get; set; }

    // Invoice PDF Template
    public string InAccentColor { get; set; } = "#374151";
    public string? InDefaultTerms { get; set; }
    public string? InFooterNote { get; set; }

    // Blank Work Order PDF Template
    public string WoAccentColor { get; set; } = "#374151";
    public string? WoTerms { get; set; }

    // Setup Wizard Progress
    public bool SetupWizardStarted { get; set; } = false;
    public bool SetupWizardCompleted { get; set; } = false;
    /// <summary>Comma-separated step numbers that have been completed (e.g. "1,2,3")</summary>
    public string? SetupWizardDoneSteps { get; set; }
    /// <summary>Comma-separated step numbers the user chose to skip</summary>
    public string? SetupWizardSkippedSteps { get; set; }
    /// <summary>UTC timestamp of when the setup wizard was completed. Null if not yet completed.</summary>
    public DateTime? SetupWizardCompletedAt { get; set; }
    /// <summary>ASP.NET Identity user ID of the user who completed the setup wizard.</summary>
    public string? SetupWizardCompletedByUserId { get; set; }
    /// <summary>Display name of the user who completed the setup wizard, stored at completion time
    /// to avoid a runtime JOIN to the Identity tables when listing companies.</summary>
    public string? SetupWizardCompletedByName { get; set; }
    /// <summary>True when the company indicated they are migrating data from QuickBooks Desktop.</summary>
    public bool MigratingFromQuickBooks { get; set; } = false;
    /// <summary>JSON blob persisting QB Migration Wizard step state across sessions.</summary>
    public string? QbMigrationStateJson { get; set; }

    // Kiosk settings
    /// <summary>
    /// Controls what the kiosk creates on submission: "Quote" (default) or "Job".
    /// Quote aligns with the default Terms text ("subject to a formal quote").
    /// Job is for shops that price on the spot and want the work order ready immediately.
    /// </summary>
    public string KioskIntakeOutput { get; set; } = "Quote";

    // Guided activation / first-workflow onboarding
    /// <summary>Selected first-workflow path: quote_first or job_first. Null until chosen.</summary>
    public string? OnboardingPath { get; set; }
    /// <summary>True once the company completes its first guided real workflow.</summary>
    public bool FirstWorkflowCompleted { get; set; } = false;
    /// <summary>UTC timestamp of when the first guided workflow was completed.</summary>
    public DateTime? FirstWorkflowCompletedAt { get; set; }
    /// <summary>UTC timestamp of the company's first quote creation.</summary>
    public DateTime? FirstQuoteCreatedAt { get; set; }
    /// <summary>UTC timestamp of the company's first job creation.</summary>
    public DateTime? FirstJobCreatedAt { get; set; }
    /// <summary>UTC timestamp of the company's first invoice creation.</summary>
    public DateTime? FirstInvoiceCreatedAt { get; set; }
    /// <summary>UTC timestamp of when the company dismissed guided activation without completing it.</summary>
    public DateTime? GuidedActivationDismissedAt { get; set; }

    // Navigation
    public virtual Company Company { get; set; } = null!;
}
