import type { User } from '@/types/auth';

export type Company = {
    id: number;
    ext_id: number;
    name: string;
    address: string;
    zip_code: string;
    city: string;
    province: string;
    vat_code: string;
    logo: string | null;
    logo_url: string | null;
    created_at: string;
    updated_at: string;

    employees?: Employee[];
    payslipErrors?: PayslipError[];
    contacts?: Contact[];
};

export type Employee = {
    id: number;
    name: string;
    fiscal_code: string | null;
    role: string | null;
    hiring_date: string | null;
    termination_date: string | null;
    company_id: number;
    created_at: string;
    updated_at: string;

    company?: Company;
    timekeepings?: Timekeeping[];
    timekeepings_count?: number;
    missing_clo?: number;
    timekeeping_years?: number[];
};

export type EmployeeRoleOption = {
    value: string;
    label: string;
    color: string;
};

export type AssetCategoryOption = {
    value: string;
    label: string;
};

export type AssetTypeOption = {
    value: string;
    label: string;
};

export type Contact = {
    id: number;
    company_id: number;
    first_name: string;
    last_name: string;
    role: string | null;
    mobile_phone: string | null;
    email: string | null;
    phone: string | null;
    created_at: string;
    updated_at: string;

    company?: Company;
};

export type Reporting = {
    id: number;
    company_id: number;
    related_year: number;
    user_id: number;
    created_at: string;
    updated_at: string;

    projects_count?: number;

    user?: User;
    company?: Company;
    projects?: Project[];
};

export type ProjectBoundary = {
    id: number;
    project_id: number;
    related_year: number;
    date_start: string;
    date_end: string;
};

export type Project = {
    id: number;
    name: string;
    status: 'preliminary' | 'consolidation' | 'final';
    reporting_id: number | null;
    simulation_id: number | null;
    user_id: number;
    created_at: string;
    updated_at: string;
    years: number[];

    user?: User;
    reporting?: Reporting;
    simulation?: Simulation;
    employees?: Employee[];
    timesheets?: Timesheet[];
    employee_costs_sum_subtotal_cost?: number;
    boundaries?: ProjectBoundary[];
};

export type CostProjectAllocation = {
    id: number;
    allocatable_type: string;
    allocatable_id: number;
    project_id: number;
    percentage: number | null;
    formula: string[] | null;
    computed_amount: number | null;
    created_at: string;
    updated_at: string;

    project?: Project;
};

export type ExtCompany = {
    Codice: number;
    Capleg: string;
    CittaLeg: string;
    IndirizzoLeg: string;
    PartitaIva: string;
    ProvinvciaLeg: string;
    RagioneSociale: string;
};

export type EmployeeCost = {
    id: number;
    project_id: number;
    related_year: number;
    name: string;
    role: string;
    worked_hours: number;
    project_hours: number;
    project_percent: number;
    clo: number;
    subtotal_cost: number;
    is_validated: boolean;
    created_at: string;
    updated_at: string;
    employee_id: number;
    original_project_hours: number;
    timesheets_count?: number;
    percentages?: Record<number, number>[];
    related_months: string[];

    project?: Project;
    timesheets?: Timesheet[];
    employee: Employee;
};

export type Timesheet = {
    id: number;
    project_id: number;
    employee_id: number;
    employee_cost_id: number;
    related_month: number;
    times: Record<string, number>;
    total_hours: number;
    created_at: string;
    updated_at: string;

    project?: Project;
    employer_cost?: EmployeeCost;
    employee?: Employee;
};

export type Area = {
    id: number;
    name: string;
    created_at: string;
    updated_at: string;
};

export type Supplier = {
    id: number;
    name: string;
    address: string;
    city: string;
    zip_code: string;
    province: string;
    vat_code: string;
    tax_code: string | null;
    email: string | null;
    phone: string | null;
    pec: string | null;
    created_at: string;
    updated_at: string;
};

export type ContractFile = {
    id: number;
    contract_id: number;
    path: string;
    original_name: string | null;
    created_at: string;
    updated_at: string;
};

export type CostDocument = {
    id: number;
    documentable_type: string;
    documentable_id: number;
    category: 'invoice' | 'accounting' | 'report' | 'attachment';
    path: string;
    original_name: string;
    created_at: string;
    updated_at: string;
};

export type Contract = {
    id: number;
    project_id: number;
    supplier_id: number;
    type: string;
    related_year: number;
    notes: string | null;
    created_at: string;
    updated_at: string;

    supplier?: Supplier;
    files?: ContractFile[];
};

export type Invoice = {
    id: number;
    supplier_id: number | null;
    manufacturer_name: string | null;
    invoice_date: string;
    invoice_number: string;
    description: string | null;
    amount: number;
    created_at: string;
    updated_at: string;

    supplier?: Supplier;
    documents?: CostDocument[];
};

export type AssetSoftwareCost = {
    id: number;
    invoice_id: number | null;
    reportable_type: string;
    reportable_id: number;
    related_year: number;
    depreciation_perc: number;
    depreciation_expense: number;
    utilization_rate_numerator: number;
    utilization_rate_denominator: number;
    utilization_rate: number;
    eligible_cost: number;
    is_patent_box: boolean;
    is_innovation: boolean;
    notes: string | null;
    created_at: string;
    updated_at: string;

    invoice: Invoice;
    allocations?: CostProjectAllocation[];
    documents?: CostDocument[];
};

export type EligibleCostDetail = {
    id: number;
    costable_type: string;
    costable_id: number;
    formula: string;
    description: string | null;
    computed_amount: number;
    created_at: string;
    updated_at: string;
};

export type EligibleCost = {
    id: number;
    invoice_id: number | null;
    reportable_type: string;
    reportable_id: number;
    related_year: number;
    eligible_cost: number;
    is_patent_box: boolean;
    is_innovation: boolean;
    notes: string | null;
    is_promiscuous: boolean;
    is_patent_box_validated: boolean;
    is_innovation_validated: boolean;
    user_id: number;
    created_at: string;
    updated_at: string;

    invoice: Invoice;
    details?: EligibleCostDetail[];
    allocations?: CostProjectAllocation[];
    documents?: CostDocument[];
};

export type SimulationReport = {
    id: number;
    simulation_id: number;
    user_id: number | null;
    version: number;
    path: string;
    original_name: string;
    report_asset_selection: Record<string, string[]> | null;
    pruned_at: string | null;
    created_at: string;
    updated_at: string;

    user?: Pick<User, 'id' | 'name'> | null;
};

export type Simulation = {
    id: number;
    company_id: number;
    related_year: number;
    years: number[];
    user_id: number;
    status: string;
    report_asset_selection: Record<string, string[]> | null;
    system_tax_rates_configured?: boolean;
    gieffe_cost_mode: 'fixed' | 'brackets' | 'brackets_capped';
    gieffe_fixed_cost: number | null;
    gieffe_cost_cap: number | null;
    created_at: string;
    updated_at: string;

    simulated_employee_costs_sum_subtotal_cost?: number;
    external_personnel_costs_sum_subtotal_cost?: number;
    asset_software_costs_sum_eligible_cost?: number;
    extra_muros_costs_sum_eligible_cost?: number;
    consulting_costs_sum_eligible_cost?: number;
    material_costs_sum_eligible_cost?: number;
    legal_costs_sum_eligible_cost?: number;

    simulated_employee_costs?: SimulatedEmployeeCost[];
    external_personnel_costs?: ExternalPersonnelCost[];
    asset_software_costs?: AssetSoftwareCost[];
    extra_muros_costs?: EligibleCost[];
    consulting_costs?: EligibleCost[];
    material_costs?: EligibleCost[];
    legal_costs?: EligibleCost[];
    projects?: Project[];
    reports?: SimulationReport[];

    user?: User;
    company?: Company;
};

export type TimekeepingDayDetail = {
    regular_hours: number;
    extra_hours: number;
    details: {
        type: string;
        hours: number;
    }[];
};

export type Timekeeping = {
    id: number;
    company_id: number;
    employee_id: number;
    related_year: number;
    related_month: number;
    times: Record<string, number>[];
    total_hours: number;
    clo: number;
    created_at: string;
    updated_at: string;
    pdf_path: string | null;
    image_path: string | null;
    full_calendar: Record<string, TimekeepingDayDetail> | null;
    daily_hours: number;
    role: string | null;
    hiring_date: string | null;
    is_validated: boolean;

    company?: Company;
    employee?: Employee;
};

export type ToBeEmployeeCost = {
    employee_id: number;
    related_year: number;
    employee: Employee;
    clo: number;
    related_months: string[];
    worked_hours: number;
};

export type SimulatedEmployeeCost = {
    id: number;
    simulation_id: number;
    related_year: number;
    name: string;
    role: string | null;
    area_id: number | null;
    clo: number;
    worked_hours: number;
    project_hours: number;
    project_percent: number;
    subtotal_cost: number;
    created_at: string;
    updated_at: string;

    area?: Area;
    allocations?: CostProjectAllocation[];
    documents?: CostDocument[];
};

export type ExternalPersonnelCost = {
    id: number;
    reportable_type: string;
    reportable_id: number;
    related_year: number;
    name: string;
    fee: number;
    project_percent: number;
    subtotal_cost: number;
    user_id: number;
    created_at: string;
    updated_at: string;

    allocations?: CostProjectAllocation[];
    documents?: CostDocument[];
    details?: EligibleCostDetail[];
};

export interface Paginator<T> {
    data: T[];
    current_page: number;
    last_page: number;
    per_page: number;
    total: number;
    from: number;
    to: number;
    next_page_url: string | null;
    links: {
        url: string | null;
        label: string;
        active: boolean;
    }[];
}

export type ExcludedPayslipAcronym = {
    id: number;
    company_id: number;
    related_year: number;
    acronym: string;
    created_at: string;
    updated_at: string;
};

export type ExcludedEmployeeAcronym = {
    id: number;
    employee_id: number;
    related_year: number;
    acronym: string;
    created_at: string;
    updated_at: string;
};

export type Bracket = {
    id: number;
    from_value: number;
    to_value: number | null;
    percentage: number;
    sort_order: number;
    created_at: string;
    updated_at: string;
};

export type TaxRate = {
    id: number;
    name: string;
    value: number | null;
    is_system: boolean;
    created_at: string;
    updated_at: string;
};

export type PayslipError = {
    id: number;
    company_id: number;
    original_filename: string;
    original_filepath: string | null;
    extracted_pdf_path: string | null;
    created_at: string;
    updated_at: string;

    company?: Company;
};

export type GieffeCost = {
    cost: number;
    details: {
        from_value: number;
        to_value: number | null;
        percentage: number;
        base: number;
        cost: number;
    }[];
    capped?: boolean;
    cap?: number;
};

export type YearlyCosts = {
    [year: number]: number;
};

export type ActivityLog = {
    id: number;
    event: string | null;
    created_at: string;
    subject_type: string;
    causer: { name: string } | null;
    changes: { field: string; old: unknown; new: unknown }[];
};

export type DetailedYearlyCosts = {
    [year: number]: {
        assetSoftwareCosts: number;
        extraMurosCosts: number;
        consultingCosts: number;
        materialCosts: number;
        legalCosts: number;
        simulatedEmployeeCosts: number;
        total: number;
        externalPersonnelCosts: number;
    };
};
