create table if not exists public.submissions ( submission_id text primary key, submitted_at timestamptz not null, student_name text not null, auth_provider text, provider_user_id text, challenge_id text, repo_url text, target_file text, attempt_number integer not null default 1, hints_used integer not null default 0, llm_score integer, total_score integer, passed_tests integer, total_tests integer, submission_outcome text, review_report text, payload jsonb not null, created_at timestamptz not null default now() ); alter table public.submissions add column if not exists review_report text; update public.submissions set review_report = concat_ws( E'\n\n', 'SUBMISSION REVIEW: ' || submission_id, 'Student: ' || coalesce(payload #>> '{student,name}', student_name), 'Hugging Face: ' || coalesce(payload #>> '{student,provider_user_id}', '(not provided)'), 'Challenge: ' || coalesce(payload #>> '{challenge,challenge_id}', challenge_id, '(not provided)'), 'Repository: ' || coalesce(payload #>> '{challenge,repo_url}', repo_url, '(not provided)'), 'What was the bug:' || E'\n' || coalesce(payload #>> '{challenge,bug_description}', '(not provided)'), 'Challenge summary:' || E'\n' || coalesce(payload #>> '{challenge,challenge_summary}', '(not provided)'), 'Buggy code given to the student:' || E'\n' || coalesce(payload #>> '{challenge,buggy_code}', '(not provided)'), 'Student changes (diff):' || E'\n' || coalesce(payload #>> '{submission,student_diff}', '(not provided)'), 'Expected fix:' || E'\n' || coalesce(payload #>> '{challenge,expected_fixed_code}', '(not provided)'), 'Test results:' || E'\n' || coalesce(jsonb_pretty(payload -> 'tests'), '(not provided)'), 'Scoring:' || E'\n' || coalesce(jsonb_pretty(payload -> 'scoring'), '(not provided)'), 'Chat history:' || E'\n' || coalesce(jsonb_pretty(payload #> '{submission,chat_history}'), '(not provided)') ) where review_report is null and payload is not null; create index if not exists submissions_provider_user_id_idx on public.submissions (provider_user_id); create index if not exists submissions_submitted_at_idx on public.submissions (submitted_at desc); create index if not exists submissions_challenge_id_idx on public.submissions (challenge_id); alter table public.submissions enable row level security; revoke all on table public.submissions from anon, authenticated; grant select, insert, update on table public.submissions to service_role;