// @vitest-environment jsdom
//
// FB-36 (P0 compliance, composition profile — ADR 013): the work_mode
// ("Arbeitsort") capture UI must be hideable via the `workModeCapture`
// profile flag. Before this flag existed the radio group always rendered
// unconditionally, so "hidden when false" below is a genuine regression
// test — against the pre-flag component it fails (3 radios would be found,
// not 0).
//
// FIXTURE-DATEN-REGEL: every id/name below is FREE INVENTION.

import { afterEach, describe, expect, it, vi } from 'vitest'
import { act, cleanup, render, screen } from '@testing-library/react'
import EditAssignmentModal from '@/components/planning/edit-assignment-modal'
import type { Consultant, AppointmentType, PlanningProject, Assignment } from '@/lib/planning-types'

// EditAssignmentModal creates a Supabase browser client unconditionally
// (`useMemo(() => createClient(), [])`, runs during render) and its child
// SupplierPicker does the same on a debounced search. createBrowserClient
// throws without real Supabase env vars (not set in the test environment),
// so every call site the component can reach needs a safe stub.
vi.mock('@/lib/supabase/client', () => {
  interface FakeChain {
    select: () => FakeChain
    eq: () => FakeChain
    order: () => Promise<{ data: unknown[]; error: null }>
    single: () => Promise<{ data: null; error: null }>
  }
  function makeChain(): FakeChain {
    const chain: FakeChain = {
      select: () => chain,
      eq: () => chain,
      order: () => Promise.resolve({ data: [], error: null }),
      single: () => Promise.resolve({ data: null, error: null }),
    }
    return chain
  }
  return {
    createClient: () => ({
      from: () => makeChain(),
      rpc: () => Promise.resolve({ data: [], error: null }),
      auth: { getUser: () => Promise.resolve({ data: { user: null }, error: null }) },
    }),
  }
})

afterEach(() => {
  cleanup()
  vi.clearAllMocks()
})

const consultants: Consultant[] = [
  {
    id: '11111111-1111-1111-1111-111111111111',
    auth_user_id: null,
    first_name: 'Erika',
    last_name: 'Musterfrau',
    display_name: 'Erika Musterfrau',
    team_code: 'T1',
    role: 'consultant',
    is_active: true,
    capacity_hours_per_day: 8,
    created_at: '2026-01-01T00:00:00Z',
  },
]

const appointmentTypes: AppointmentType[] = [
  { id: '22222222-2222-2222-2222-222222222222', code: 'visit', label: 'Besuch', color: '#037493', sort_order: 1 },
]

const planningProjects: PlanningProject[] = [
  {
    id: '33333333-3333-3333-3333-333333333333',
    code: 'P-1',
    name: 'Projekt Eins',
    supplier_id: null,
    location_label: null,
    country: null,
    is_active: true,
    created_at: '2026-01-01T00:00:00Z',
  },
]

const assignment: Assignment = {
  id: '44444444-4444-4444-4444-444444444444',
  date: '2026-07-01',
  start_time: null,
  end_time: null,
  project_id: null,
  consultant_id: consultants[0].id,
  description: null,
  appointment_type_id: appointmentTypes[0].id,
  supplier_name: null,
  supplier_id: null,
  location_label: null,
  status: 'fixed',
  work_mode: 'onsite',
  is_all_day: true,
  requires_travel: false,
  source: 'manual',
  created_at: '2026-01-01T00:00:00Z',
  updated_at: '2026-01-01T00:00:00Z',
  created_by: null,
  updated_by: null,
}

async function renderModal(workModeCapture: boolean) {
  const result = render(
    <EditAssignmentModal
      assignment={assignment}
      consultants={consultants}
      appointmentTypes={appointmentTypes}
      planningProjects={planningProjects}
      onClose={() => {}}
      onSaved={() => {}}
      onDeleted={() => {}}
      workModeCapture={workModeCapture}
    />,
  )
  await act(async () => {})
  return result
}

describe('EditAssignmentModal — workModeCapture flag (FB-36)', () => {
  it('hides the Arbeitsort radio group when the flag is false', async () => {
    await renderModal(false)
    expect(screen.queryByText('Arbeitsort *')).toBeNull()
    expect(screen.queryAllByRole('radio')).toHaveLength(0)
  })

  it('shows the Arbeitsort radio group when the flag is true', async () => {
    await renderModal(true)
    expect(screen.getByText('Arbeitsort *')).toBeTruthy()
    expect(screen.getAllByRole('radio')).toHaveLength(3)
  })
})
