using System;

namespace JobTrackerApi.Models
{
public class JobApplication
{
    public int Id { get; set; }
    public string? OwnerUserId { get; set; }
    public string JobTitle { get; set; } = "";
    public int CompanyId { get; set; }
    public Company Company { get; set; } = null!;

    // The opportunity this application is for. Nullable for legacy rows; new creates and edits
    // keep Job synchronized while JobApplication remains the current read model.
    // See JobTrackerApi/Models/Job.cs and docs/decisions/ADR-002-job-application-model.md.
    public int? JobId { get; set; }
    public Job? Job { get; set; }

    public string Status { get; set; } = "Applied";

    /// <summary>
    /// When the user submitted the application. Null while the job is still in a pre-application
    /// (Prospect) stage — Saved/Interested/Preparing — because nothing has been submitted yet.
    /// Callers must not synthesise a date for unapplied jobs: a fake DateApplied feeds the
    /// follow-up/ghosting rules and the applied-volume analytics.
    /// </summary>
    public DateTime? DateApplied { get; set; }

    /// <summary>When the user first captured this job. Always set.</summary>
    public DateTime SavedAt { get; set; } = DateTime.UtcNow;

    public string? Location { get; set; }
    public string? Salary { get; set; }

    // Structured salary; the free-text Salary field is kept for display/back-compat.
    public decimal? SalaryMin { get; set; }
    public decimal? SalaryMax { get; set; }
    public string? SalaryCurrency { get; set; } // e.g. "NOK", "GBP", "EUR"
    public string? SalaryPeriod { get; set; }   // "year" | "month" | "hour"
    public string? NextAction { get; set; }
    public DateTime? FollowUpAt { get; set; }
    public DateTime? FeedbackRequestedAt { get; set; }
    public string? RecruiterMessageDraft { get; set; }

    // Attachment checklist. Derived from Attachment rows, not directly settable by API
    // consumers -- see AttachmentsController.RecomputeAttachmentFlagsAsync, the single place
    // these are written, so they can't drift from what's actually attached.
    public bool HasResume { get; set; } = false;
    public bool HasCoverLetter { get; set; } = false;
    public bool HasPortfolio { get; set; } = false;
    public bool HasOtherAttachment { get; set; } = false;

    // Soft delete: hide from default queries without losing history.
    public bool IsDeleted { get; set; } = false;
    public DateTime? DeletedAt { get; set; }

    public bool ResponseReceived { get; set; } = false;
    public DateTime? ResponseDate { get; set; }
    public string? Notes { get; set; }
    public string? CoverLetterText { get; set; }
    public string? JobUrl { get; set; }

    // Imported job content
    public string? Description { get; set; }
    public string? TranslatedDescription { get; set; }
    public string? DescriptionLanguage { get; set; } // "en", "no", ...
    public string? Tags { get; set; } // JSON array string, e.g. ["Azure","Docker"]
    public DateTime? Deadline { get; set; }
    // Short summary generated at creation time and persisted to avoid repeated model calls.
    public string? ShortSummary { get; set; }
    public string? TailoredCvText { get; set; }
    public DateTime? TailoredCvUpdatedAt { get; set; }
    public DateTime? LastReminderEmailSentAt { get; set; }

    public TailoredCvDraft? TailoredCvDraft { get; set; }
    public List<Correspondence> Messages { get; set; } = new();
    public List<Attachment> Attachments { get; set; } = new();
    public List<JobEvent> Events { get; set; } = new();

    /// <summary>
    /// Days since the application was submitted. Null for pre-application (Prospect) stages:
    /// with no DateApplied there is no elapsed time to report, and 0 would read as
    /// "applied today".
    /// </summary>
    public int? DaysSince => DateApplied is null
        ? null
        : ((ResponseReceived ? (ResponseDate ?? DateTime.UtcNow) : DateTime.UtcNow) - DateApplied.Value.ToUniversalTime()).Days;
}
}
