/**
 * Reading sheets from Google into the CRM.
 *
 * Three sheets can be connected at once — the client list, the job sheet and
 * the installer register. The page shows, for each, what the last read found
 * (rows ready, rows skipped and why) before anything is written, and the
 * switches that keep it in sync.
 */
export type SheetImportSource = 'clients' | 'jobs' | 'installers';

/**
 * The counts a report carries. Each importer fills the ones that mean
 * something to it: a job sheet counts new customers and sites, the installer
 * register counts updates and rows under a "not used any more" heading.
 */
export type SheetImportStats = {
    rows?: number;
    ready?: number;
    updates?: number;
    duplicates?: number;
    same_site_new_componentry?: number;
    missing_name?: number;
    missing_contact?: number;
    missing_address?: number;
    invalid_address?: number;
    new_customers?: number;
    existing_customers?: number;
    new_sites?: number;
    existing_sites?: number;
    inactive?: number;
    sections?: number;
    componentry_missing?: number;
};

export type SheetImportRow = {
    row: number | null;
    /** The tab a row came from, for a register spread over a tab per state. */
    sheet?: string | null;
    name: string | null;
    address: string | null;
    outcome: string | null;
    reason: string | null;
};

export type SheetImportReport = {
    imported_at: string | null;
    source: string | null;
    sheet: string | null;
    committed: boolean;
    stats: SheetImportStats;
    created?: {
        customers?: number;
        sites?: number;
        jobs?: number;
        created?: number;
        updated?: number;
    };
    rows: SheetImportRow[];
    /** How many rows the report holds, of which `rows` is the first page. */
    row_count: number;
};

export type SheetImportLastRun = {
    at: string;
    committed: boolean;
    stats: SheetImportStats;
    created: { customers?: number; sites?: number; jobs?: number; created?: number; updated?: number };
};

export type SheetImportSourceSettings = {
    source: SheetImportSource;
    label: string;
    description: string;
    url: string | null;
    spreadsheet_id: string | null;
    enabled: boolean;
    commits: boolean;
    cron: string;
    last_run: SheetImportLastRun | null;
    /** The last dry run for this sheet. */
    preview: SheetImportReport | null;
    /** The last real import for this sheet. */
    import: SheetImportReport | null;
    /** The command the scheduler runs, shown so it can be run by hand. */
    command: string;
};

export type SheetImportProps = {
    sources: SheetImportSourceSettings[];
    schedules: Array<{ value: string; label: string }>;
    can: { manage: boolean };
};
