'use client';

import type { FunConfigDashboard } 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 { Label } from '@/components/ui/label';
import { Switch } from '@/components/ui/switch';
import type { SettingsSaveResult } from '@/components/settings/settings-form';
import { SettingsForm } from '@/components/settings/settings-form';

async function saveFun(guildId: string, value: FunConfigDashboard): Promise<SettingsSaveResult> {
  try {
    const response = await fetch(`/api/guilds/${guildId}/fun`, {
      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 FunFormProps {
  guildId: string;
  initialValue: FunConfigDashboard;
}

export function FunForm({ guildId, initialValue }: FunFormProps) {
  const t = useTranslations();

  return (
    <SettingsForm<FunConfigDashboard> initialValue={initialValue} onSave={(value) => saveFun(guildId, value)}>
      {({ value, setValue }) => (
        <Card>
          <CardHeader>
            <CardTitle>{t('modulePages.fun.title')}</CardTitle>
            <CardDescription>{t('modulePages.fun.description')}</CardDescription>
          </CardHeader>
          <CardContent className="space-y-4">
            <FieldAnchor id="field-fun-enabled">
            <div className="flex items-center justify-between rounded-md border border-border p-4">
              <div className="space-y-0.5">
                <Label>{t('modulePages.fun.enabledLabel')}</Label>
                <p className="text-sm text-muted-foreground">{t('modulePages.fun.enabledHint')}</p>
              </div>
              <Switch
                checked={value.enabled}
                onCheckedChange={(checked) => setValue((prev) => ({ ...prev, enabled: checked }))}
              />
            </div>
            </FieldAnchor>
            <FieldAnchor id="field-fun-media">
            <div className="flex items-center justify-between rounded-md border border-border p-4">
              <div className="space-y-0.5">
                <Label>{t('modulePages.fun.mediaEnabledLabel')}</Label>
                <p className="text-sm text-muted-foreground">{t('modulePages.fun.mediaEnabledHint')}</p>
              </div>
              <Switch
                checked={value.mediaEnabled}
                onCheckedChange={(checked) => setValue((prev) => ({ ...prev, mediaEnabled: checked }))}
              />
            </div>
            </FieldAnchor>
          </CardContent>
        </Card>
      )}
    </SettingsForm>
  );
}
