import type {
  Company, CompanyAuditEvent, CompanyClaim, CompanyMembership,
} from '@/types/db'

/**
 * DESIGN-PREVIEW fixtures for the Company page (Task 83, 2026-09-08).
 *
 * Nash: "Can we have the design done so we can present to the team before we
 * do the back end? Can we have the flow of how to request a verification, how
 * you get connected… if I'm logged in with an email that is proven to be the
 * email of the company that has a pending request — should I have an approve
 * or deny ability? If my email is not linked to any company, what buttons do
 * I have?"
 *
 * These rows never touch the database. They exist so every state of the
 * flow can be walked through on the current test logins — with or without
 * migration 0013 — exactly like the "Preview state" switcher on /billing.
 * Names and emails read as demo on purpose.
 */

const NOW = '2026-09-08T14:00:00.000Z'
const DEMO_COMPANY_ID = '00000000-0000-4000-8000-000000000c01'

export const PREVIEW_COMPANY: Company = {
  id: DEMO_COMPANY_ID,
  legal_name: 'ABC Freight Brokers LLC',
  display_name: 'ABC Freight Brokers',
  dba_name: null,
  company_type: 'broker',
  mc_number: '123456',
  dot_number: '2233445',
  physical_address: '100 Main St, Suite 200, Dallas, TX 75201',
  mailing_address: null,
  corporate_email: 'verification-demo+mc123456@heavyhaulagent.test',
  corporate_phone: '(555) 010-0100',
  website: 'https://abcfreight.example',
  logo_url: null,
  verification_status: 'corporate_contact_confirmed',
  verification_level: 2,
  source_fmcsa_id: null,
  source_sos_id: null,
  agents_see_all_trips: false,
  notification_recipients: [],
  locked: false,
  merged_into: null,
  is_placeholder: false,
  created_at: NOW,
  updated_at: NOW,
}

export const PREVIEW_CARRIER: Company = {
  ...PREVIEW_COMPANY,
  id: '00000000-0000-4000-8000-000000000c02',
  legal_name: 'D&D Transporting LLC',
  display_name: 'D&D Transporting',
  company_type: 'carrier',
  mc_number: '088374',
  dot_number: '3106896',
  physical_address: '13717 S Route 30 Unit 155, Plainfield, IL 60544',
  corporate_email: 'verification-demo+mc088374@heavyhaulagent.test',
  website: null,
}

export function previewClaim(overrides: Partial<CompanyClaim> = {}): CompanyClaim {
  return {
    id: '00000000-0000-4000-8000-00000000cl01',
    user_id: 'preview-user',
    company_id: DEMO_COMPANY_ID,
    requested_role: 'broker_agent',
    claim_status: 'pending_corporate_approval',
    verification_method: 'corporate_email_approval',
    mc_number: '123456',
    approval_sent_to: PREVIEW_COMPANY.corporate_email,
    approval_token: 'preview-token',
    expires_at: '2026-09-22T14:00:00.000Z',
    approver_name: null,
    approver_email: null,
    approver_ip: null,
    decided_at: null,
    review_reason: null,
    review_notes: null,
    info_request: null,
    created_at: NOW,
    updated_at: NOW,
    ...overrides,
  }
}

export function previewMembership(overrides: Partial<CompanyMembership> = {}): CompanyMembership {
  return {
    id: '00000000-0000-4000-8000-00000000mb01',
    user_id: 'preview-user',
    company_id: DEMO_COMPANY_ID,
    role: 'broker_agent',
    status: 'approved',
    approved_by: 'Jane Owner',
    approved_at: NOW,
    revoked_by: null,
    revoked_at: null,
    created_at: NOW,
    ...overrides,
  }
}

/** Requests a signed-in corporate contact would see waiting for a decision. */
export const PREVIEW_APPROVALS = [
  {
    claim: previewClaim({ id: '00000000-0000-4000-8000-00000000cl11', user_id: 'preview-req-1' }),
    company: PREVIEW_COMPANY,
    requester: { name: 'Maria Lopez', email: 'maria.lopez@abcfreight.example', phone: '(555) 010-0111' },
  },
  {
    claim: previewClaim({
      id: '00000000-0000-4000-8000-00000000cl12',
      user_id: 'preview-req-2',
      mc_number: '123456',
      created_at: '2026-09-07T09:30:00.000Z',
    }),
    company: PREVIEW_COMPANY,
    requester: { name: 'Sam Driver', email: 'sam.d@gmail.com', phone: null },
  },
]

/** Members shown on the Company Access tab in the admin preview. */
export const PREVIEW_MEMBERS = [
  {
    membership: previewMembership({ id: 'mb-owner', user_id: 'preview-owner', role: 'company_owner', approved_by: 'corporate email' }),
    claim: null,
    name: 'Jane Owner',
    email: 'jane@abcfreight.example',
    phone: '(555) 010-0101',
  },
  {
    membership: previewMembership({ id: 'mb-agent', user_id: 'preview-agent', role: 'broker_agent' }),
    claim: previewClaim({ id: 'cl-agent', user_id: 'preview-agent', claim_status: 'approved', decided_at: NOW }),
    name: 'Tom Agent',
    email: 'tom@abcfreight.example',
    phone: null,
  },
  {
    membership: previewMembership({ id: 'mb-pending', user_id: 'preview-req-1', status: 'pending', approved_by: null, approved_at: null }),
    claim: PREVIEW_APPROVALS[0].claim,
    name: 'Maria Lopez',
    email: 'maria.lopez@abcfreight.example',
    phone: '(555) 010-0111',
  },
]

export const PREVIEW_AUDIT: CompanyAuditEvent[] = [
  {
    id: 'au-1',
    actor_user_id: null,
    actor_label: 'Jane Owner <jane@abcfreight.example>',
    company_id: DEMO_COMPANY_ID,
    event_type: 'claim_approved',
    old_value: null,
    new_value: { user: 'Tom Agent', role: 'broker_agent', via: 'corporate_email' },
    ip_address: null,
    created_at: NOW,
  },
  {
    id: 'au-2',
    actor_user_id: null,
    actor_label: 'Maria Lopez',
    company_id: DEMO_COMPANY_ID,
    event_type: 'claim_created',
    old_value: null,
    new_value: { mc: '123456', sent_to: PREVIEW_COMPANY.corporate_email },
    ip_address: null,
    created_at: '2026-09-08T13:00:00.000Z',
  },
]
