'use client'

import { trpc } from '@/lib/trpc/client'
import { Switch } from '@/components/ui/switch'
import { Label } from '@/components/ui/label'
import { toast } from 'sonner'

/**
 * Admin toggle: whether finalist teams may upload *revised* grand-final documents.
 * Off by default — judges always see the teams' existing prior-round submissions
 * regardless; this only controls whether teams are prompted/allowed to upload new
 * revised versions (and whether the upload reminder cron runs).
 */
export function FinalDocsUploadsToggle({ roundId }: { roundId: string }) {
  const utils = trpc.useUtils()
  const { data } = trpc.finalist.getRevisedUploadSetting.useQuery({ roundId })
  const set = trpc.finalist.setRevisedUploadSetting.useMutation({
    onSuccess: (r) => {
      toast.success(r.enabled ? 'Finalist revised uploads enabled' : 'Finalist revised uploads disabled')
      utils.finalist.getRevisedUploadSetting.invalidate({ roundId })
    },
    onError: (e) => toast.error(e.message),
  })
  return (
    <div className="flex items-center gap-2">
      <Switch
        id="finalist-revised-uploads"
        checked={!!data?.enabled}
        disabled={set.isPending}
        onCheckedChange={(v) => set.mutate({ roundId, enabled: v })}
      />
      <Label htmlFor="finalist-revised-uploads" className="text-sm text-muted-foreground cursor-pointer">
        Allow finalists to upload revised documents
      </Label>
    </div>
  )
}
