create table if not exists public.practice_session_reports (
  id uuid primary key default gen_random_uuid(),
  tenant_id uuid not null references public.tenants(id) on delete cascade,
  user_id uuid not null references public.platform_users(id) on delete cascade,
  practice_session_id uuid not null references public.practice_sessions(id) on delete cascade,
  blueprint_id uuid references public.practice_blueprints(id) on delete set null,
  collection_id uuid references public.question_collections(id) on delete set null,
  mode text not null default 'mock_exam',
  total_questions integer not null default 0 check (total_questions >= 0),
  answered_count integer not null default 0 check (answered_count >= 0),
  correct_count integer not null default 0 check (correct_count >= 0),
  wrong_count integer not null default 0 check (wrong_count >= 0),
  unanswered_count integer not null default 0 check (unanswered_count >= 0),
  score numeric(10,2) not null default 0 check (score >= 0),
  total_score numeric(10,2) not null default 0 check (total_score >= 0),
  accuracy numeric(6,4) not null default 0 check (accuracy >= 0 and accuracy <= 1),
  duration_seconds integer not null default 0 check (duration_seconds >= 0),
  started_at timestamptz,
  submitted_at timestamptz not null default now(),
  section_stats jsonb not null default '[]'::jsonb,
  question_results jsonb not null default '[]'::jsonb,
  wrong_question_ids jsonb not null default '[]'::jsonb,
  metadata jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now(),
  updated_at timestamptz not null default now(),
  unique (tenant_id, practice_session_id)
);

create table if not exists public.practice_session_report_sections (
  id uuid primary key default gen_random_uuid(),
  tenant_id uuid not null references public.tenants(id) on delete cascade,
  report_id uuid not null references public.practice_session_reports(id) on delete cascade,
  practice_session_id uuid not null references public.practice_sessions(id) on delete cascade,
  section_key text not null,
  section_name text,
  question_type text,
  question_count integer not null default 0 check (question_count >= 0),
  answered_count integer not null default 0 check (answered_count >= 0),
  correct_count integer not null default 0 check (correct_count >= 0),
  wrong_count integer not null default 0 check (wrong_count >= 0),
  unanswered_count integer not null default 0 check (unanswered_count >= 0),
  score numeric(10,2) not null default 0 check (score >= 0),
  total_score numeric(10,2) not null default 0 check (total_score >= 0),
  accuracy numeric(6,4) not null default 0 check (accuracy >= 0 and accuracy <= 1),
  sort_order integer not null default 0,
  metadata jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now(),
  updated_at timestamptz not null default now(),
  unique (tenant_id, report_id, section_key)
);

create index if not exists idx_practice_session_reports_user
  on public.practice_session_reports(tenant_id, user_id, submitted_at desc);

create index if not exists idx_practice_session_reports_blueprint
  on public.practice_session_reports(tenant_id, blueprint_id, submitted_at desc)
  where blueprint_id is not null;

create index if not exists idx_practice_session_report_sections_report
  on public.practice_session_report_sections(tenant_id, report_id, sort_order);

do $$
declare
  table_name text;
begin
  foreach table_name in array array['practice_session_reports', 'practice_session_report_sections']
  loop
    execute format('alter table public.%I enable row level security', table_name);
    execute format('drop policy if exists tenant_isolation on public.%I', table_name);
    execute format(
      'create policy tenant_isolation on public.%I for all using (tenant_id = app.current_tenant_id() or app.is_platform_admin()) with check (tenant_id = app.current_tenant_id() or app.is_platform_admin())',
      table_name
    );
    execute format('drop trigger if exists set_updated_at on public.%I', table_name);
    execute format('create trigger set_updated_at before update on public.%I for each row execute function app.touch_updated_at()', table_name);
  end loop;
end $$;
