'use client';

import type { VerificationConfigDashboard } from '@nexumi/shared';
import { FieldAnchor } from '@/components/layout/field-anchor';
import { useTranslations } from '@/components/locale-provider';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { DiscordChannelSelect } from '@/components/ui/discord-channel-select';
import { DiscordRoleSelect } from '@/components/ui/discord-role-select';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
import { Switch } from '@/components/ui/switch';
import type { SettingsSaveResult } from '@/components/settings/settings-form';
import { SettingsForm } from '@/components/settings/settings-form';

async function saveVerification(guildId: string, value: VerificationConfigDashboard): Promise<SettingsSaveResult> {
  try {
    const response = await fetch(`/api/guilds/${guildId}/verification`, {
      method: 'PATCH',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(value)
    });
    if (!response.ok) {
      const body = (await response.json().catch(() => null)) as { error?: string } | null;
      return { ok: false, error: body?.error ?? `Request failed with status ${response.status}` };
    }
    return { ok: true };
  } catch {
    return { ok: false, error: 'Network error' };
  }
}

interface VerificationFormProps {
  guildId: string;
  initialValue: VerificationConfigDashboard;
}

export function VerificationForm({ guildId, initialValue }: VerificationFormProps) {
  const t = useTranslations();

  return (
    <SettingsForm<VerificationConfigDashboard>
      initialValue={initialValue}
      onSave={(value) => saveVerification(guildId, value)}
    >
      {({ value, setValue }) => (
        <Card>
          <CardHeader>
            <CardTitle>{t('modulePages.verification.title')}</CardTitle>
            <CardDescription>{t('modulePages.verification.description')}</CardDescription>
          </CardHeader>
          <CardContent className="space-y-4">
            <FieldAnchor id="field-verify-enabled">
            <div className="flex items-center justify-between rounded-md border border-border p-4">
              <Label>{t('modulePages.verification.enabledLabel')}</Label>
              <Switch
                checked={value.enabled}
                onCheckedChange={(checked) => setValue((prev) => ({ ...prev, enabled: checked }))}
              />
            </div>
            </FieldAnchor>

            <div className="grid gap-4 sm:grid-cols-2">
              <FieldAnchor id="field-verify-mode">
              <div className="space-y-2">
                <Label>{t('modulePages.verification.mode')}</Label>
                <Select
                  value={value.mode}
                  onValueChange={(mode) =>
                    setValue((prev) => ({ ...prev, mode: mode as VerificationConfigDashboard['mode'] }))
                  }
                >
                  <SelectTrigger>
                    <SelectValue />
                  </SelectTrigger>
                  <SelectContent>
                    <SelectItem value="BUTTON">{t('modulePages.verification.mode.BUTTON')}</SelectItem>
                    <SelectItem value="CAPTCHA">{t('modulePages.verification.mode.CAPTCHA')}</SelectItem>
                  </SelectContent>
                </Select>
              </div>
              </FieldAnchor>
              <FieldAnchor id="field-verify-fail">
              <div className="space-y-2">
                <Label>{t('modulePages.verification.failAction')}</Label>
                <Select
                  value={value.failAction}
                  onValueChange={(failAction) =>
                    setValue((prev) => ({ ...prev, failAction: failAction as VerificationConfigDashboard['failAction'] }))
                  }
                >
                  <SelectTrigger>
                    <SelectValue />
                  </SelectTrigger>
                  <SelectContent>
                    <SelectItem value="KICK">{t('modulePages.verification.failAction.KICK')}</SelectItem>
                    <SelectItem value="BAN">{t('modulePages.verification.failAction.BAN')}</SelectItem>
                    <SelectItem value="NONE">{t('modulePages.verification.failAction.NONE')}</SelectItem>
                  </SelectContent>
                </Select>
              </div>
              </FieldAnchor>
            </div>

            <div className="grid gap-4 sm:grid-cols-3">
              <FieldAnchor id="field-verify-channel">
              <div className="space-y-2">
                <Label>{t('modulePages.verification.channelId')}</Label>
                <DiscordChannelSelect
                  guildId={guildId}
                  value={value.channelId}
                  onChange={(channelId) => setValue((prev) => ({ ...prev, channelId }))}
                />
              </div>
              </FieldAnchor>
              <FieldAnchor id="field-verify-role">
              <div className="space-y-2">
                <Label>{t('modulePages.verification.verifiedRoleId')}</Label>
                <DiscordRoleSelect
                  guildId={guildId}
                  value={value.verifiedRoleId}
                  onChange={(verifiedRoleId) => setValue((prev) => ({ ...prev, verifiedRoleId }))}
                />
              </div>
              </FieldAnchor>
              <FieldAnchor id="field-verify-unverified">
              <div className="space-y-2">
                <Label>{t('modulePages.verification.unverifiedRoleId')}</Label>
                <DiscordRoleSelect
                  guildId={guildId}
                  value={value.unverifiedRoleId}
                  onChange={(unverifiedRoleId) => setValue((prev) => ({ ...prev, unverifiedRoleId }))}
                />
              </div>
              </FieldAnchor>
            </div>

            <FieldAnchor id="field-verify-age">
            <div className="space-y-2">
              <Label>{t('modulePages.verification.minAccountAgeDays')}</Label>
              <Input
                type="number"
                min={0}
                className="w-40"
                value={value.minAccountAgeDays}
                onChange={(event) =>
                  setValue((prev) => ({ ...prev, minAccountAgeDays: Number(event.target.value) || 0 }))
                }
              />
            </div>
            </FieldAnchor>
          </CardContent>
        </Card>
      )}
    </SettingsForm>
  );
}
