"use client";

import { useState } from "react";
import {
  type WholesaleSettings,
  type NotificationRecipient,
  saveWholesaleSettings,
} from "@/actions/wholesale";
import { AdminButton } from "@/components/admin/design-system";

import AddRecipientForm from "./AddRecipientForm";
import WebhookSettingsSection from "./WebhookSettingsSection";

type SettingsForm = {
  requireApproval: boolean;
  minOrderAmount: string;
  onlinePaymentEnabled: boolean;
  wholesaleEnabled: boolean;
  squareSyncEnabled: boolean;
  pickupLocation: string;
  fobLocation: string;
  fromEmail: string;
  invoiceBusinessName: string;
  invoiceBusinessAddress: string;
  invoiceBusinessPhone: string;
  invoiceBusinessEmail: string;
  invoiceBusinessWebsite: string;
  notificationRecipients: NotificationRecipient[];
};

function settingsFormFromSettings(settings: WholesaleSettings | null): SettingsForm {
  return {
    requireApproval: settings?.require_approval ?? true,
    minOrderAmount: settings?.min_order_amount != null ? String(settings.min_order_amount) : "",
    onlinePaymentEnabled: settings?.online_payment_enabled ?? false,
    wholesaleEnabled: settings?.wholesale_enabled ?? true,
    squareSyncEnabled: settings?.square_sync_enabled ?? false,
    pickupLocation: settings?.pickup_location ?? "",
    fobLocation: settings?.fob_location ?? "",
    fromEmail: settings?.from_email ?? "",
    invoiceBusinessName: settings?.invoice_business_name ?? "",
    invoiceBusinessAddress: settings?.invoice_business_address ?? "",
    invoiceBusinessPhone: settings?.invoice_business_phone ?? "",
    invoiceBusinessEmail: settings?.invoice_business_email ?? "",
    invoiceBusinessWebsite: settings?.invoice_business_website ?? "",
    notificationRecipients: settings?.notification_recipients ?? [],
  };
}

export default function SettingsTab({ settings, brandId, onMsg, onRefresh, canManageSettings }: {
  settings: WholesaleSettings | null;
  brandId: string;
  onMsg: (kind: "success" | "error", text: string) => void;
  onRefresh: () => void;
  canManageSettings: boolean;
}) {
  // Hooks must be called unconditionally - always declare them first
  const [form, setForm] = useState<SettingsForm>(() => settingsFormFromSettings(settings));
  const [saving, setSaving] = useState(false);

  // Permission check after hooks
  if (!canManageSettings) {
    return (
      <div className="flex items-center justify-center h-64">
        <p className="text-slate-400">You do not have permission to manage settings.</p>
      </div>
    );
  }

  async function handleSave() {
    setSaving(true);
    const result = await saveWholesaleSettings({
      brandId,
      requireApproval: form.requireApproval,
      minOrderAmount: form.minOrderAmount ? Number(form.minOrderAmount) : undefined,
      onlinePaymentEnabled: form.onlinePaymentEnabled,
      wholesaleEnabled: form.wholesaleEnabled,
      squareSyncEnabled: form.squareSyncEnabled,
      pickupLocation: form.pickupLocation,
      fobLocation: form.fobLocation,
      fromEmail: form.fromEmail,
      invoiceBusinessName: form.invoiceBusinessName,
      invoiceBusinessAddress: form.invoiceBusinessAddress,
      invoiceBusinessPhone: form.invoiceBusinessPhone,
      invoiceBusinessWebsite: form.invoiceBusinessWebsite,
      notificationRecipients: form.notificationRecipients,
    });
    setSaving(false);
    if (result.success) {
      onMsg("success", "Settings saved.");
      onRefresh();
    } else {
      onMsg("error", result.error ?? "Failed to save settings.");
    }
  }

  function setField<K extends keyof SettingsForm>(field: K, value: SettingsForm[K]) {
    setForm(f => ({ ...f, [field]: value }));
  }

  // ── Notification Recipients helpers ────────────────────────────────────────
  function addRecipient(email: string, name: string = "") {
    setForm(f => ({
      ...f,
      notificationRecipients: [
        ...f.notificationRecipients,
        { email, name, active: true },
      ],
    }));
  }

  function removeRecipient(idx: number) {
    setForm(f => ({
      ...f,
      notificationRecipients: f.notificationRecipients.filter((_: unknown, i: number) => i !== idx),
    }));
  }

  function toggleRecipient(idx: number) {
    setForm(f => ({
      ...f,
      notificationRecipients: f.notificationRecipients.map((r: NotificationRecipient, i: number) =>
        i === idx ? { ...r, active: !r.active } : r
      ),
    }));
  }

  function updateRecipientName(idx: number, name: string) {
    setForm(f => ({
      ...f,
      notificationRecipients: f.notificationRecipients.map((r: NotificationRecipient, i: number) =>
        i === idx ? { ...r, name } : r
      ),
    }));
  }

  return (
    <div className="space-y-4">
      <h2 className="text-lg font-semibold text-[var(--admin-text-primary)]">Wholesale Settings</h2>

      <div className="rounded-2xl bg-white border border-[var(--admin-border)] p-6 shadow-sm">
        <div className="space-y-4">
          <div className="flex items-center justify-between">
            <div>
              <p className="font-medium text-[var(--admin-text-primary)]">Require Approval</p>
              <p className="text-sm text-[var(--admin-text-muted)]">New registrations must be manually approved.</p>
            </div>
            <button type="button"
              onClick={() => setField("requireApproval", !form.requireApproval)}
              aria-label="Toggle require approval"
              className={`relative inline-flex h-7 w-12 items-center rounded-full transition-colors ${form.requireApproval ? "bg-[var(--admin-accent)]" : "bg-[var(--admin-border)]"}`}
            >
              <span className={`inline-block h-5 w-5 transform rounded-full bg-white shadow transition-transform ${form.requireApproval ? "translate-x-6" : "translate-x-1"}`} />
            </button>
          </div>

          <div className="flex items-center justify-between">
            <div>
              <p className="font-medium text-[var(--admin-text-primary)]">Wholesale Portal</p>
              <p className="text-sm text-[var(--admin-text-muted)]">Show the Wholesale Portal card on this brand&apos;s storefront page.</p>
            </div>
            <button type="button"
              onClick={() => setField("wholesaleEnabled", !form.wholesaleEnabled)}
              aria-label="Toggle wholesale portal"
              className={`relative inline-flex h-7 w-12 items-center rounded-full transition-colors ${form.wholesaleEnabled ? "bg-[var(--admin-accent)]" : "bg-[var(--admin-border)]"}`}
            >
              <span className={`inline-block h-5 w-5 transform rounded-full bg-white shadow transition-transform ${form.wholesaleEnabled ? "translate-x-6" : "translate-x-1"}`} />
            </button>
          </div>

          <div className="flex items-center justify-between rounded-xl border border-[var(--admin-border)] bg-[var(--admin-bg-subtle)] px-4 py-3">
            <div className="flex items-center gap-3">
              <div className="flex h-8 w-8 items-center justify-center rounded-lg bg-[var(--admin-accent-light)]">
                <svg className="h-4 w-4 text-[var(--admin-accent)]" fill="none" viewBox="0 0 24 24" strokeWidth={2} stroke="currentColor">
                  <path strokeLinecap="round" strokeLinejoin="round" d="M7 16V4m0 0L3 8m4-4l4 4m6 0v12m0 0l4-4m-4 4l-4-4" />
                </svg>
              </div>
              <div>
                <p className="font-medium text-[var(--admin-text-primary)]">Square Sync for Wholesale</p>
                <p className="text-xs text-[var(--admin-text-muted)]">
                  When enabled, wholesale product changes are automatically pushed to Square.
                  {form.squareSyncEnabled
                    ? " Auto-sync is active."
                    : " Auto-sync is disabled — changes will not be pushed to Square."}
                </p>
              </div>
            </div>
            <button type="button"
              onClick={() => setField("squareSyncEnabled", !form.squareSyncEnabled)}
              aria-label="Toggle square sync"
              className={`relative inline-flex h-7 w-12 items-center rounded-full transition-colors ${form.squareSyncEnabled ? "bg-[var(--admin-accent)]" : "bg-[var(--admin-border)]"}`}
            >
              <span className={`inline-block h-5 w-5 transform rounded-full bg-white shadow transition-transform ${form.squareSyncEnabled ? "translate-x-6" : "translate-x-1"}`} />
            </button>
          </div>

          <div className="grid grid-cols-2 gap-4">
            <div>
              <label htmlFor="fld-12-minimum-order-amount"  className="block text-sm font-medium text-[var(--admin-text-secondary)] mb-1">Minimum Order Amount ($)</label>
              <input id="fld-12-minimum-order-amount"  aria-label="No Minimum" type="number" value={form.minOrderAmount} onChange={e => setField("minOrderAmount", e.target.value)}
                className="w-full rounded-xl border border-[var(--admin-border)] px-3 py-2 text-sm outline-none focus:border-[var(--admin-accent)]" step="0.01" placeholder="No minimum" />
            </div>
            <div>
              <label htmlFor="fld-13-from-email"  className="block text-sm font-medium text-[var(--admin-text-secondary)] mb-1">From Email</label>
              <input id="fld-13-from-email"  aria-label="Email" type="email" value={form.fromEmail} onChange={e => setField("fromEmail", e.target.value)}
                className="w-full rounded-xl border border-[var(--admin-border)] px-3 py-2 text-sm outline-none focus:border-[var(--admin-accent)]" />
            </div>
            <div className="col-span-2">
              <label htmlFor="fld-14-pickup-location"  className="block text-sm font-medium text-[var(--admin-text-secondary)] mb-1">Pickup Location</label>
              <textarea id="fld-14-pickup-location"  aria-label="Textarea" value={form.pickupLocation} onChange={e => setField("pickupLocation", e.target.value)}
                className="w-full rounded-xl border border-[var(--admin-border)] px-3 py-2 text-sm outline-none focus:border-[var(--admin-accent)] font-mono" rows={3} />
            </div>
            <div className="col-span-2">
              <label htmlFor="fld-15-fob-location"  className="block text-sm font-medium text-[var(--admin-text-secondary)] mb-1">FOB Location</label>
              <input id="fld-15-fob-location"  aria-label="Input" value={form.fobLocation} onChange={e => setField("fobLocation", e.target.value)}
                className="w-full rounded-xl border border-[var(--admin-border)] px-3 py-2 text-sm outline-none focus:border-[var(--admin-accent)]" />
            </div>
            <div className="col-span-2">
              <label htmlFor="fld-16-invoice-business-name"  className="block text-sm font-medium text-[var(--admin-text-secondary)] mb-1">Invoice Business Name</label>
              <input id="fld-16-invoice-business-name"  aria-label="Input" value={form.invoiceBusinessName} onChange={e => setField("invoiceBusinessName", e.target.value)}
                className="w-full rounded-xl border border-[var(--admin-border)] px-3 py-2 text-sm outline-none focus:border-[var(--admin-accent)]" />
            </div>
            <div className="col-span-2">
              <label htmlFor="fld-17-invoice-business-address"  className="block text-sm font-medium text-[var(--admin-text-secondary)] mb-1">Invoice Business Address</label>
              <textarea id="fld-17-invoice-business-address"  aria-label="Textarea" value={form.invoiceBusinessAddress} onChange={e => setField("invoiceBusinessAddress", e.target.value)}
                className="w-full rounded-xl border border-[var(--admin-border)] px-3 py-2 text-sm outline-none focus:border-[var(--admin-accent)]" rows={2} />
            </div>
            <div>
              <label htmlFor="fld-18-invoice-business-phone"  className="block text-sm font-medium text-[var(--admin-text-secondary)] mb-1">Invoice Business Phone</label>
              <input id="fld-18-invoice-business-phone"  aria-label="Input" value={form.invoiceBusinessPhone} onChange={e => setField("invoiceBusinessPhone", e.target.value)}
                className="w-full rounded-xl border border-[var(--admin-border)] px-3 py-2 text-sm outline-none focus:border-[var(--admin-accent)]" />
            </div>
            <div>
              <label htmlFor="fld-19-invoice-business-email"  className="block text-sm font-medium text-[var(--admin-text-secondary)] mb-1">Invoice Business Email</label>
              <input id="fld-19-invoice-business-email"  aria-label="Email" type="email" value={form.invoiceBusinessEmail} onChange={e => setField("invoiceBusinessEmail", e.target.value)}
                className="w-full rounded-xl border border-[var(--admin-border)] px-3 py-2 text-sm outline-none focus:border-[var(--admin-accent)]" />
            </div>
            <div className="col-span-2">
              <label htmlFor="fld-20-invoice-business-website"  className="block text-sm font-medium text-[var(--admin-text-secondary)] mb-1">Invoice Business Website</label>
              <input id="fld-20-invoice-business-website"  aria-label="Https://" value={form.invoiceBusinessWebsite} onChange={e => setField("invoiceBusinessWebsite", e.target.value)}
                className="w-full rounded-xl border border-[var(--admin-border)] px-3 py-2 text-sm outline-none focus:border-[var(--admin-accent)]" placeholder="https://" />
            </div>

            {/* Team Notification Recipients */}
            <div className="col-span-2 rounded-2xl bg-[var(--admin-bg-subtle)] p-5 ring-1 ring-[var(--admin-border)]">
              <label className="block text-sm font-semibold text-[var(--admin-text-primary)] mb-1" htmlFor="fld-team-notification-recipients">Team Notification Recipients</label>
              <p className="text-xs text-[var(--admin-text-muted)] mb-4 leading-relaxed">
                These team members receive all wholesale notifications for this brand — new orders,
                deposits, fulfillments, price sheets, and pickup reminders. If no recipients are
                active, the system falls back to any configured notification email on file.
              </p>

              {/* Empty state */}
              {form.notificationRecipients.length === 0 && (
                <div className="text-center py-6 mb-4 rounded-xl bg-white ring-1 ring-[var(--admin-border)]">
                  <p className="text-sm text-[var(--admin-text-muted)] mb-1">No recipients added yet.</p>
                  <p className="text-xs text-[var(--admin-text-muted)]">Add an email address below to get started.</p>
                </div>
              )}

              {/* Recipients list */}
              <div className="space-y-2 mb-4">
                {form.notificationRecipients.map((r: NotificationRecipient, idx: number) => (
                  <div key={r.email || `recipient-${idx}`} className={`flex items-center gap-2 rounded-xl px-3 py-2.5 bg-white ring-1 ${r.active ? "ring-[var(--admin-border)]" : "ring-[var(--admin-border-light)] opacity-70"}`}>
                    <input id="fld-team-notification-recipients" aria-label="Checkbox"
                      type="checkbox" checked={r.active} onChange={() => toggleRecipient(idx)}
                      className="rounded border-[var(--admin-border)] mt-0.5" title={r.active ? "Active — receives notifications" : "Inactive"} />
                    <div className="flex-1 min-w-0">
                      <p className={`text-sm font-medium ${r.active ? "text-[var(--admin-text-primary)]" : "text-[var(--admin-text-muted)]"}`}>{r.email}</p>
                      {r.name && <p className="text-xs text-[var(--admin-text-muted)] truncate">{r.name}</p>}
                      {r.notification_types && r.notification_types.length > 0 && (
                        <p className="text-xs text-[var(--admin-text-secondary)] mt-0.5" title="Per-type filtering coming soon">Types: {r.notification_types.join(", ")}</p>
                      )}
                    </div>
                    <input aria-label="Name (optional)"
                      type="text" placeholder="Name (optional)"
                      value={r.name ?? ""}
                      onChange={e => updateRecipientName(idx, e.target.value)}
                      className="rounded-xl border border-[var(--admin-border)] px-2.5 py-1.5 text-xs w-36 outline-none focus:border-[var(--admin-accent)]" />
                    <button type="button" onClick={() => removeRecipient(idx)}
                      aria-label="Remove recipient"
                      className="text-[var(--admin-danger)] hover:text-[var(--admin-danger)] text-xs font-medium px-2 py-1 rounded-lg hover:bg-[var(--admin-danger-light)] transition-colors"
                      title="Remove recipient">
                      <svg className="h-3.5 w-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2.5}>
                        <path strokeLinecap="round" strokeLinejoin="round" d="M6 18L18 6M6 6l12 12" />
                      </svg>
                    </button>
                  </div>
                ))}
              </div>

              {/* Add recipient */}
              <AddRecipientForm onAdd={addRecipient} />
            </div>
            {/* Webhook Settings */}
            <div className="col-span-2 rounded-2xl bg-[var(--admin-bg-subtle)] p-5 ring-1 ring-[var(--admin-border)]">
              <label className="block text-sm font-semibold text-[var(--admin-text-primary)] mb-1" htmlFor="fld-webhook-integration">Webhook Integration</label>
              <p className="text-xs text-[var(--admin-text-muted)] mb-4 leading-relaxed">
                Send order events to external systems (Harvest Point, ERPs, etc.). Payload is signed
                with HMAC-SHA256. Enable and configure the URL and secret below.
              </p>
              <WebhookSettingsSection brandId={brandId} onMsg={onMsg} />
            </div>

          </div>

          <AdminButton onClick={handleSave} disabled={saving} variant="primary" isLoading={saving}>
            {saving ? "Saving..." : "Save Settings"}
          </AdminButton>
        </div>
      </div>
    </div>
  );
}
