'use client';
import { formatErrorBanner } from '@/lib/api/toast-error';

import { useState } from 'react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Switch } from '@/components/ui/switch';
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from '@/components/ui/select';
import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetFooter } from '@/components/ui/sheet';
import { TimezoneCombobox } from '@/components/shared/timezone-combobox';
import { apiFetch } from '@/lib/api/client';

// ISO-4217 currency shortlist for the port-currency dropdown.
// Marina deals price in a small set; an admin who needs an exotic
// currency can add it here without a schema change.
const CURRENCY_OPTIONS: Array<{ value: string; label: string }> = [
  { value: 'USD', label: 'USD - US Dollar' },
  { value: 'EUR', label: 'EUR - Euro' },
  { value: 'GBP', label: 'GBP - British Pound' },
  { value: 'CHF', label: 'CHF - Swiss Franc' },
  { value: 'AED', label: 'AED - UAE Dirham' },
  { value: 'SAR', label: 'SAR - Saudi Riyal' },
  { value: 'PLN', label: 'PLN - Polish Złoty' },
  { value: 'AUD', label: 'AUD - Australian Dollar' },
  { value: 'CAD', label: 'CAD - Canadian Dollar' },
  { value: 'NZD', label: 'NZD - New Zealand Dollar' },
  { value: 'JPY', label: 'JPY - Japanese Yen' },
];

interface PortFormProps {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  port?: {
    id: string;
    name: string;
    slug: string;
    logoUrl: string | null;
    primaryColor: string | null;
    defaultCurrency: string;
    timezone: string;
    isActive: boolean;
  } | null;
  onSuccess: () => void;
}

export function PortForm(props: PortFormProps) {
  return (
    <PortFormBody key={props.open ? `open:${props.port?.id ?? 'new'}` : 'closed'} {...props} />
  );
}

function PortFormBody({ open, onOpenChange, port, onSuccess }: PortFormProps) {
  const [name, setName] = useState(port?.name ?? '');
  const [slug, setSlug] = useState(port?.slug ?? '');
  const [primaryColor, setPrimaryColor] = useState(port?.primaryColor ?? '#0F4C81');
  const [defaultCurrency, setDefaultCurrency] = useState(port?.defaultCurrency ?? 'USD');
  const [timezone, setTimezone] = useState(port?.timezone ?? 'America/Anguilla');
  const [isActive, setIsActive] = useState(port?.isActive ?? true);
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);

  const isEdit = !!port;

  function handleNameChange(value: string) {
    setName(value);
    if (!isEdit) {
      setSlug(
        value
          .toLowerCase()
          .replace(/[^a-z0-9]+/g, '-')
          .replace(/^-|-$/g, ''),
      );
    }
  }

  async function handleSubmit(e: React.FormEvent) {
    e.preventDefault();
    setError(null);
    setLoading(true);

    try {
      if (isEdit) {
        await apiFetch(`/api/v1/admin/ports/${port.id}`, {
          method: 'PATCH',
          body: { name, slug, primaryColor, defaultCurrency, timezone, isActive },
        });
      } else {
        await apiFetch('/api/v1/admin/ports', {
          method: 'POST',
          body: { name, slug, primaryColor, defaultCurrency, timezone },
        });
      }
      onSuccess();
      onOpenChange(false);
    } catch (err: unknown) {
      const message = formatErrorBanner(err);
      setError(message);
    } finally {
      setLoading(false);
    }
  }

  return (
    <Sheet open={open} onOpenChange={onOpenChange}>
      <SheetContent className="overflow-y-auto">
        <SheetHeader>
          <SheetTitle>{isEdit ? 'Edit Port' : 'New Port'}</SheetTitle>
        </SheetHeader>

        <form onSubmit={handleSubmit} className="mt-6 space-y-4">
          <div className="space-y-2">
            <Label htmlFor="port-name">Name</Label>
            <Input
              id="port-name"
              value={name}
              onChange={(e) => handleNameChange(e.target.value)}
              placeholder="Port Nimara"
              required
            />
          </div>

          <div className="space-y-2">
            <Label htmlFor="port-slug">Slug</Label>
            <Input
              id="port-slug"
              value={slug}
              onChange={(e) => setSlug(e.target.value)}
              placeholder="port-nimara"
              pattern="^[a-z0-9-]+$"
              required
            />
            <p className="text-xs text-muted-foreground">
              Used in URLs. Lowercase letters, numbers, and hyphens only.
            </p>
          </div>

          <div className="space-y-2">
            <Label htmlFor="port-color">Brand Color</Label>
            <div className="flex items-center gap-2">
              <input
                type="color"
                id="port-color"
                value={primaryColor}
                onChange={(e) => setPrimaryColor(e.target.value)}
                className="h-9 w-9 rounded border cursor-pointer"
              />
              <Input
                value={primaryColor}
                onChange={(e) => setPrimaryColor(e.target.value)}
                placeholder="#0F4C81"
                className="w-28 font-mono text-sm"
                maxLength={7}
              />
            </div>
          </div>

          <div className="grid grid-cols-2 gap-4">
            <div className="space-y-2">
              <Label htmlFor="port-currency">Currency</Label>
              <Select value={defaultCurrency} onValueChange={setDefaultCurrency}>
                <SelectTrigger id="port-currency">
                  <SelectValue />
                </SelectTrigger>
                <SelectContent>
                  {CURRENCY_OPTIONS.map((c) => (
                    <SelectItem key={c.value} value={c.value}>
                      {c.label}
                    </SelectItem>
                  ))}
                </SelectContent>
              </Select>
            </div>

            <div className="space-y-2">
              <Label>Timezone</Label>
              <TimezoneCombobox value={timezone} onChange={(tz) => setTimezone(tz ?? '')} />
            </div>
          </div>

          {isEdit && (
            <div className="flex items-center justify-between rounded-lg border p-3">
              <div>
                <Label htmlFor="port-active">Port Active</Label>
                <p className="text-xs text-muted-foreground">
                  Inactive ports are hidden from users
                </p>
              </div>
              <Switch id="port-active" checked={isActive} onCheckedChange={setIsActive} />
            </div>
          )}

          {error && <p className="whitespace-pre-line text-sm text-destructive">{error}</p>}

          <SheetFooter>
            <Button
              type="button"
              variant="outline"
              onClick={() => onOpenChange(false)}
              disabled={loading}
            >
              Cancel
            </Button>
            <Button type="submit" disabled={loading || !name.trim() || !slug.trim()}>
              {loading ? 'Saving…' : isEdit ? 'Save changes' : 'Create Port'}
            </Button>
          </SheetFooter>
        </form>
      </SheetContent>
    </Sheet>
  );
}
