import { readFileSync } from 'node:fs';
import { describe, expect, it } from 'vitest';

const source = readFileSync(
  new URL('../src/actions/contractReportActions.ts', import.meta.url),
  'utf8'
);

describe('contractReportActions expiration report wiring', () => {
  it('exposes decision_due_date alongside end_date when available', () => {
    expect(source).toContain('export interface ContractExpiration {');
    expect(source).toContain('decision_due_date?: string | null;');
    expect(source).toContain("'cc.decision_due_date',");
    expect(source).toContain("decision_due_date: row.decision_due_date ? new Date(row.decision_due_date).toISOString().split('T')[0] : null,");
    expect(source).toContain('end_date: endDate.toISOString().split(\'T\')[0],');
  });

  it('includes renewal_mode in expiration report row payloads', () => {
    expect(source).toContain("renewal_mode?: 'none' | 'manual' | 'auto' | null;");
    expect(source).toContain("'cc.renewal_mode',");
    expect(source).toContain("'cc.use_tenant_renewal_defaults',");
    expect(source).toContain("'dbs.default_renewal_mode as tenant_default_renewal_mode',");
    expect(source).toContain('const effectiveRenewalMode =');
    expect(source).toContain('renewal_mode: effectiveRenewalMode,');
  });

  it('includes queue status in expiration rows when work-item status exists', () => {
    expect(source).toContain("import type { RenewalWorkItemStatus } from '@alga-psa/types';");
    expect(source).toContain('queue_status?: RenewalWorkItemStatus | null;');
    expect(source).toContain("'cc.status as queue_status',");
    expect(source).toContain('queue_status: row.queue_status ?? null,');
  });

  it('derives expiration rows from assignment lifecycle and groups them by client assignment id', () => {
    expect(source).toContain("'cc.is_active',");
    expect(source).toContain('const assignmentStatus = deriveClientContractStatus({');
    expect(source).toContain("if (assignmentStatus !== 'active') {");
    expect(source).toContain('const key = row.client_contract_id;');
    expect(source).toContain('const expirationMap = new Map<string, ContractExpiration>();');
    expect(source).toContain('existing.monthly_value += row.monthly_value || 0;');
  });

  it('keeps fixed-term expiration rows even when renewal fields are unset', () => {
    expect(source).toContain(".whereNotNull('cc.end_date')");
    expect(source).not.toContain(".whereNotNull('cc.renewal_mode')");
    expect(source).toContain("const useTenantRenewalDefaults = row.use_tenant_renewal_defaults !== false;");
    expect(source).toContain("?? 'manual';");
  });

  it('keeps expiration-report sort semantics compatible with existing consumers', () => {
    expect(source).toContain(".orderBy('cc.end_date', 'asc');");
    expect(source).toContain('const key = row.client_contract_id;');
  });

  it('derives legacy auto_renew from effective renewal mode values', () => {
    expect(source).toContain('auto_renew: effectiveRenewalMode === \'auto\'');
  });
});
