import type { FakeSupabase, Row } from './fake-supabase'
import type { BillingContext } from '@/lib/integrations/synchron-payments/types'
import type { Trip } from '@/types/db'

/**
 * Seed helpers shared by tests/purchases-*.test.ts. Pure data — the vi.mock
 * wiring lives in each test file because vi.mock is hoisted per file.
 */

export const USER_ID = '11111111-1111-4111-8111-111111111111'
export const COMPANY_A = 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa'
export const COMPANY_B = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'

/** Migration 0045 catalog: express $1.99, extended $10.00. */
export function seedCatalog(db: FakeSupabase) {
  db.seed('routing_products', [
    { product_code: 'express_route', product_name: 'Express Route', selling_price_cents: 199 },
    { product_code: 'extended_route', product_name: 'Extended Route', selling_price_cents: 1000 },
  ])
}

export function seedTrip(db: FakeSupabase, extra: Row = {}): Trip {
  const [trip] = db.seed('trips', [{
    ref_code: 'HH-20260929',
    created_by: USER_ID,
    permit_policy: 'upload_allowed',
    synchron_order_token: null,
    carrier_company_id: null,
    ...extra,
  }])
  return trip as unknown as Trip
}

/** One uploaded permit per state, none processed by Synchron. */
export function seedPermits(db: FakeSupabase, tripId: string, states: string[]): Row[] {
  return db.seed('permits', states.map((state_code) => ({
    trip_id: tripId, state_code, source_system: 'upload', route_processing_status: null, route_links: null,
  })))
}

export function billingFor(trip: Trip, opts: { companyId?: string | null } = {}): BillingContext {
  return {
    hhaUserId: USER_ID,
    hhaCompanyId: opts.companyId ?? null,
    synchronUserToken: 'USERTOKEN1234567',
    synchronCarrierToken: null,
    synchronOrderToken: trip.synchron_order_token ?? null,
    hhaTripId: trip.id,
  }
}

export const ACTOR = { userId: USER_ID, actorLabel: 'Dana Dispatcher' }
export const APP_ORIGIN = 'https://workspace.test'
