import { useEffect, useState } from 'react';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { Save, Folder, Clock, Image, Cloud, Users } from 'lucide-react';
import { api, type AppSettings } from '@/lib/api';
import { Button } from '@/components/ui/Button';
import { Input } from '@/components/ui/Input';
import { ThemeToggle } from '@/components/ui/ThemeToggle';
import { useThemeStore, ACCENT_TOKENS, type AccentColor } from '@/store/themeStore';
import { clsx } from 'clsx';
import { Link } from 'react-router-dom';

function Section({ title, icon, children }: { title: string; icon: React.ReactNode; children: React.ReactNode }) {
  return (
    <div className="bg-surface rounded-2xl border border-theme p-6">
      <div className="flex items-center gap-3 mb-5">
        <span className="text-accent">{icon}</span>
        <h2 className="text-base font-semibold text-primary">{title}</h2>
      </div>
      <div className="space-y-4">{children}</div>
    </div>
  );
}

export default function SettingsPage() {
  const qc = useQueryClient();
  const { accent, setAccent } = useThemeStore();

  const { data: settings } = useQuery<AppSettings>({
    queryKey: ['settings'],
    queryFn: () => api.get('/settings').then((r) => r.data),
  });

  const [form, setForm] = useState<Partial<AppSettings>>({});
  useEffect(() => { if (settings) setForm(settings); }, [settings]);

  const mutation = useMutation({
    mutationFn: (patch: Partial<AppSettings>) => api.patch('/settings', patch).then((r) => r.data),
    onSuccess: () => qc.invalidateQueries({ queryKey: ['settings'] }),
  });

  const set = (key: keyof AppSettings, value: string) => setForm((f) => ({ ...f, [key]: value }));
  const save = () => mutation.mutate(form as AppSettings);

  return (
    <div className="p-6 max-w-3xl mx-auto space-y-6">
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-2xl font-bold text-primary">Settings</h1>
          <p className="text-secondary text-sm mt-1">Configure your family dashboard</p>
        </div>
        <Button onClick={save} loading={mutation.isPending}>
          <Save size={16} /> Save Changes
        </Button>
      </div>

      {/* ── Appearance ─────────────────────────────────────────────── */}
      <Section title="Appearance" icon={<Image size={20} />}>
        <div>
          <p className="text-sm font-medium text-secondary mb-3">Theme Mode</p>
          <ThemeToggle showLabel />
        </div>
        <div>
          <p className="text-sm font-medium text-secondary mb-3">Accent Color</p>
          <div className="flex gap-3 flex-wrap">
            {(Object.keys(ACCENT_TOKENS) as AccentColor[]).map((key) => {
              const { base, label } = ACCENT_TOKENS[key];
              return (
                <button
                  key={key}
                  onClick={() => { setAccent(key); set('accent', key); }}
                  className={clsx(
                    'flex items-center gap-2 px-4 py-2 rounded-xl border-2 text-sm font-medium transition-all',
                    accent === key
                      ? 'border-accent text-accent bg-accent-light'
                      : 'border-theme text-secondary hover:border-accent/50'
                  )}
                >
                  <span className="h-3.5 w-3.5 rounded-full shrink-0" style={{ backgroundColor: base }} />
                  {label}
                </button>
              );
            })}
          </div>
        </div>
      </Section>

      {/* ── Family Members ─────────────────────────────────────────── */}
      <Section title="Family Members" icon={<Users size={20} />}>
        <p className="text-sm text-secondary">
          Add, edit, or remove family members. Members are used throughout the app to assign chores, events, and shopping items.
        </p>
        <Link to="/settings/members">
          <Button variant="secondary">
            <Users size={16} /> Manage Family Members
          </Button>
        </Link>
      </Section>

      {/* ── Photo Slideshow ────────────────────────────────────────── */}
      <Section title="Photo Slideshow" icon={<Folder size={20} />}>
        <Input
          label="Photo Folder Path"
          value={form.photo_folder ?? ''}
          onChange={(e) => set('photo_folder', e.target.value)}
          placeholder="C:\Users\YourName\Pictures\Family"
          hint="Absolute path to the folder containing your photos. Subfolders are included."
        />
        <div className="grid grid-cols-2 gap-4">
          <div>
            <label className="text-sm font-medium text-secondary block mb-1.5">Transition Speed (ms)</label>
            <select
              className="w-full rounded-lg border border-theme bg-surface-raised px-3 py-2 text-sm text-primary focus:outline-none focus:ring-2 ring-accent"
              value={form.slideshow_speed ?? '6000'}
              onChange={(e) => set('slideshow_speed', e.target.value)}
            >
              {[3000, 5000, 6000, 8000, 10000, 15000].map((v) => (
                <option key={v} value={v}>{v / 1000}s</option>
              ))}
            </select>
          </div>
          <div>
            <label className="text-sm font-medium text-secondary block mb-1.5">Photo Order</label>
            <select
              className="w-full rounded-lg border border-theme bg-surface-raised px-3 py-2 text-sm text-primary focus:outline-none focus:ring-2 ring-accent"
              value={form.slideshow_order ?? 'random'}
              onChange={(e) => set('slideshow_order', e.target.value)}
            >
              <option value="random">Random</option>
              <option value="sequential">Sequential</option>
              <option value="newest">Newest First</option>
            </select>
          </div>
        </div>
        <div>
          <label className="text-sm font-medium text-secondary block mb-1.5">Idle Timeout (before screensaver)</label>
          <select
            className="w-full rounded-lg border border-theme bg-surface-raised px-3 py-2 text-sm text-primary focus:outline-none focus:ring-2 ring-accent"
            value={form.idle_timeout ?? '120000'}
            onChange={(e) => set('idle_timeout', e.target.value)}
          >
            <option value="60000">1 minute</option>
            <option value="120000">2 minutes</option>
            <option value="300000">5 minutes</option>
            <option value="600000">10 minutes</option>
            <option value="0">Disabled</option>
          </select>
        </div>
      </Section>

      {/* ── Date & Time ────────────────────────────────────────────── */}
      <Section title="Date & Time" icon={<Clock size={20} />}>
        <div className="grid grid-cols-2 gap-4">
          <div>
            <label className="text-sm font-medium text-secondary block mb-1.5">Time Format</label>
            <select
              className="w-full rounded-lg border border-theme bg-surface-raised px-3 py-2 text-sm text-primary focus:outline-none focus:ring-2 ring-accent"
              value={form.time_format ?? '12h'}
              onChange={(e) => set('time_format', e.target.value)}
            >
              <option value="12h">12-hour (3:30 PM)</option>
              <option value="24h">24-hour (15:30)</option>
            </select>
          </div>
          <div>
            <label className="text-sm font-medium text-secondary block mb-1.5">Date Format</label>
            <select
              className="w-full rounded-lg border border-theme bg-surface-raised px-3 py-2 text-sm text-primary focus:outline-none focus:ring-2 ring-accent"
              value={form.date_format ?? 'MM/DD/YYYY'}
              onChange={(e) => set('date_format', e.target.value)}
            >
              <option value="MM/DD/YYYY">MM/DD/YYYY</option>
              <option value="DD/MM/YYYY">DD/MM/YYYY</option>
              <option value="YYYY-MM-DD">YYYY-MM-DD</option>
            </select>
          </div>
        </div>
      </Section>

      {/* ── Weather ────────────────────────────────────────────────── */}
      <Section title="Weather Widget" icon={<Cloud size={20} />}>
        <Input
          label="OpenWeatherMap API Key"
          value={form.weather_api_key ?? ''}
          onChange={(e) => set('weather_api_key', e.target.value)}
          placeholder="Your free API key from openweathermap.org"
          type="password"
          autoComplete="new-password"
        />
        <Input
          label="Location (city name or zip)"
          value={form.weather_location ?? ''}
          onChange={(e) => set('weather_location', e.target.value)}
          placeholder="New York, US"
          autoComplete="off"
        />
        <div>
          <label className="text-sm font-medium text-secondary block mb-1.5">Units</label>
          <select
            className="w-full rounded-lg border border-theme bg-surface-raised px-3 py-2 text-sm text-primary focus:outline-none focus:ring-2 ring-accent"
            value={form.weather_units ?? 'imperial'}
            onChange={(e) => set('weather_units', e.target.value)}
          >
            <option value="imperial">Imperial (°F)</option>
            <option value="metric">Metric (°C)</option>
          </select>
        </div>
      </Section>
    </div>
  );
}
