-- Create demo jury member
INSERT INTO "User" (id, email, name, role, status, "passwordHash", "mustSetPassword", "passwordSetAt", "onboardingCompletedAt", "expertiseTags", "notificationPreference", "createdAt", "updatedAt")
VALUES (
  'demo-jury-member-001',
  'jury.demo@monaco-opc.com',
  'Dr. Marie Laurent',
  'JURY_MEMBER',
  'ACTIVE',
  '$2b$12$xUQpxLay9.0CJ08GvXrjm.yls.bp0Yeaa4TF5b4kLsIJGLrVMCVZ.',
  false,
  NOW(),
  NOW(),
  ARRAY['Marine Biology', 'Ocean Conservation', 'Sustainable Innovation'],
  'EMAIL',
  NOW(),
  NOW()
)
ON CONFLICT (email) DO UPDATE SET
  "passwordHash" = EXCLUDED."passwordHash",
  "mustSetPassword" = false,
  status = 'ACTIVE',
  "onboardingCompletedAt" = NOW(),
  "updatedAt" = NOW();

-- Get the user ID
DO $$
DECLARE
  jury_id TEXT;
  round_id TEXT;
  proj RECORD;
  form_exists BOOLEAN;
BEGIN
  SELECT id INTO jury_id FROM "User" WHERE email = 'jury.demo@monaco-opc.com';
  RAISE NOTICE 'Jury user ID: %', jury_id;

  -- Get round
  SELECT id INTO round_id FROM "Round" WHERE slug = 'mopc-2026-round-1';
  RAISE NOTICE 'Round ID: %', round_id;

  IF round_id IS NULL THEN
    RAISE EXCEPTION 'Round not found!';
  END IF;

  -- Open voting window
  UPDATE "Round" SET
    status = 'ACTIVE',
    "votingStartAt" = NOW() - INTERVAL '7 days',
    "votingEndAt" = NOW() + INTERVAL '30 days'
  WHERE id = round_id;
  RAISE NOTICE 'Voting window opened';

  -- Assign 8 projects
  FOR proj IN
    SELECT id, title FROM "Project" WHERE "roundId" = round_id ORDER BY "createdAt" DESC LIMIT 8
  LOOP
    INSERT INTO "Assignment" (id, "userId", "projectId", "roundId", method, "isRequired", "isCompleted", "createdAt")
    VALUES (
      'demo-assign-' || substr(proj.id, 1, 15),
      jury_id,
      proj.id,
      round_id,
      'MANUAL',
      true,
      false,
      NOW()
    )
    ON CONFLICT ("userId", "projectId", "roundId") DO NOTHING;
    RAISE NOTICE 'Assigned: %', proj.title;
  END LOOP;

  -- Check if evaluation form exists
  SELECT EXISTS(SELECT 1 FROM "EvaluationForm" WHERE "roundId" = round_id) INTO form_exists;

  IF NOT form_exists THEN
    INSERT INTO "EvaluationForm" (id, "roundId", name, "isActive", "criteriaJson", "createdAt", "updatedAt")
    VALUES (
      'demo-eval-form-001',
      round_id,
      'Round 1 Evaluation',
      true,
      '[{"id":"innovation","label":"Innovation & Originality","description":"How innovative is the proposed solution?","scale":10,"weight":25,"required":true},{"id":"feasibility","label":"Technical Feasibility","description":"Is the solution technically viable?","scale":10,"weight":25,"required":true},{"id":"impact","label":"Environmental Impact","description":"What is the potential positive impact on ocean health?","scale":10,"weight":30,"required":true},{"id":"team","label":"Team Capability","description":"Does the team have the skills to execute?","scale":10,"weight":20,"required":true}]'::jsonb,
      NOW(),
      NOW()
    );
    RAISE NOTICE 'Created evaluation form';
  ELSE
    RAISE NOTICE 'Evaluation form already exists';
  END IF;

  RAISE NOTICE '========================================';
  RAISE NOTICE 'Setup complete!';
  RAISE NOTICE 'Email: jury.demo@monaco-opc.com';
  RAISE NOTICE 'Password: JuryDemo2026!';
  RAISE NOTICE '========================================';
END $$;
