<script>
  import { onMount } from 'svelte';
  import {
    fetchSites,
    createSite,
    updateSite,
    deleteSite,
    fetchAccounts,
  } from '$lib/api/organization.js';
  import { success as toastSuccess, error as toastError } from '$lib/utils/toast.js';
  import DataTable from '$lib/components/DataTable.svelte';
  import Modal from '$lib/components/Modal.svelte';
  import { Plus, Edit2, Trash2, ArrowLeft, Search, LandPlot, Check } from 'lucide-svelte';

  let loading = $state(false);
  let items = $state([]);
  let accounts = $state([]);
  let modalOpen = $state(false);
  let modalMode = $state('create');
  let saving = $state(false);
  let formData = $state({
    SiteID: null,
    SiteCode: '',
    SiteName: '',
    AccountID: null,
  });
  let deleteConfirmOpen = $state(false);
  let deleteItem = $state(null);
  let deleting = $state(false);
  let searchQuery = $state('');

  // Site code validation (must be exactly 2 characters)
  let siteCodeLength = $derived(formData.SiteCode?.length || 0);
  let isSiteCodeValid = $derived(siteCodeLength === 2);
  let siteCodeBadgeClass = $derived(
    siteCodeLength === 0
      ? 'badge-ghost'
      : isSiteCodeValid
        ? 'badge-success'
        : 'badge-error'
  );

  const columns = [
    { key: 'SiteCode', label: 'Code', class: 'font-medium' },
    { key: 'SiteName', label: 'Name' },
    { key: 'AccountName', label: 'Account', class: 'w-48' },
    { key: 'actions', label: 'Actions', class: 'w-32 text-center' },
  ];

  onMount(async () => {
    await loadAccounts();
    await loadItems();
  });

  async function loadAccounts() {
    try {
      const response = await fetchAccounts();
      accounts = Array.isArray(response.data) ? response.data : [];
    } catch (err) {
      accounts = [];
    }
  }

  async function loadItems() {
    loading = true;
    try {
      const response = await fetchSites();
      items = Array.isArray(response.data) ? response.data : [];
    } catch (err) {
      toastError(err.message || 'Failed to load sites');
      items = [];
    } finally {
      loading = false;
    }
  }

  const itemsWithAccountName = $derived(
    items.map((item) => ({
      ...item,
      AccountName: accounts.find((a) => a.id === item.AccountID)?.AccountName || '-',
    }))
  );

  const filteredItems = $derived(
    itemsWithAccountName.filter((item) => {
      if (!searchQuery.trim()) return true;
      const query = searchQuery.toLowerCase();
      return (
        (item.SiteCode && item.SiteCode.toLowerCase().includes(query)) ||
        (item.SiteName && item.SiteName.toLowerCase().includes(query))
      );
    })
  );

  function openCreateModal() {
    modalMode = 'create';
    formData = { SiteID: null, SiteCode: '', SiteName: '', AccountID: null };
    modalOpen = true;
  }

  function openEditModal(row) {
    modalMode = 'edit';
    formData = {
      SiteID: row.id || row.SiteID,
      SiteCode: row.SiteCode,
      SiteName: row.SiteName,
      AccountID: row.AccountID,
    };
    modalOpen = true;
  }

  async function handleSave() {
    if (!formData.SiteCode.trim()) {
      toastError('Site code is required');
      return;
    }
    if (formData.SiteCode.trim().length !== 2) {
      toastError('Site code must be exactly 2 characters');
      return;
    }
    if (!formData.SiteName.trim()) {
      toastError('Site name is required');
      return;
    }

    saving = true;
    try {
      if (modalMode === 'create') {
        await createSite(formData);
        toastSuccess('Site created successfully');
      } else {
        await updateSite(formData);
        toastSuccess('Site updated successfully');
      }
      modalOpen = false;
      await loadItems();
    } catch (err) {
      toastError(err.message || 'Failed to save site');
    } finally {
      saving = false;
    }
  }

  function confirmDelete(row) {
    deleteItem = row;
    deleteConfirmOpen = true;
  }

  async function handleDelete() {
    deleting = true;
    try {
      const siteId = deleteItem.id || deleteItem.SiteID;
      await deleteSite(siteId);
      toastSuccess('Site deleted successfully');
      deleteConfirmOpen = false;
      deleteItem = null;
      await loadItems();
    } catch (err) {
      toastError(err.message || 'Failed to delete site');
    } finally {
      deleting = false;
    }
  }
</script>

<div class="p-4">
  <div class="flex items-center gap-4 mb-6">
    <a href="/master-data/organization" class="btn btn-ghost btn-circle">
      <ArrowLeft class="w-5 h-5" />
    </a>
    <div class="flex-1">
      <h1 class="text-xl font-bold text-gray-800">Sites</h1>
      <p class="text-sm text-gray-600">Manage organization sites</p>
    </div>
    <button class="btn btn-primary" onclick={openCreateModal}>
      <Plus class="w-4 h-4 mr-2" />
      Add Site
    </button>
  </div>

  <div class="bg-base-100 rounded-lg shadow border border-base-200">
    <div class="p-4 border-b border-base-200">
      <div class="flex items-center gap-3 max-w-md">
        <label class="input input-sm input-bordered w-full flex items-center gap-2">
          <Search class="w-4 h-4 text-gray-400" />
          <input
            type="text"
            class="grow"
            placeholder="Search by code or name..."
            bind:value={searchQuery}
          />
        </label>
        {#if searchQuery}
          <button class="btn btn-ghost btn-sm" onclick={() => (searchQuery = '')}>
            Clear
          </button>
        {/if}
      </div>
    </div>

    {#if !loading && filteredItems.length === 0}
      <div class="flex flex-col items-center justify-center py-16 px-4">
        <div class="w-16 h-16 rounded-full bg-base-200 flex items-center justify-center mb-4">
          <LandPlot class="w-8 h-8 text-gray-400" />
        </div>
        <h3 class="text-base font-semibold text-base-content mb-1">
          {searchQuery ? 'No sites found' : 'No sites yet'}
        </h3>
        <p class="text-sm text-base-content/60 text-center max-w-sm mb-4">
          {searchQuery
            ? `No sites match your search "${searchQuery}". Try a different search term.`
            : 'Get started by adding your first site.'}
        </p>
        {#if !searchQuery}
          <button class="btn btn-primary btn-sm" onclick={openCreateModal}>
            <Plus class="w-4 h-4 mr-2" />
            Add Your First Site
          </button>
        {/if}
      </div>
    {:else}
      <DataTable
        {columns}
        data={filteredItems}
        {loading}
        emptyMessage="No sites found"
        hover={true}
        bordered={false}
      >
        {#snippet cell({ column, row })}
          {#if column.key === 'actions'}
            <div class="flex justify-center gap-2">
              <button class="btn btn-sm btn-ghost" onclick={() => openEditModal(row)} aria-label="Edit {row.SiteName}">
                <Edit2 class="w-4 h-4" />
              </button>
              <button class="btn btn-sm btn-ghost text-error" onclick={() => confirmDelete(row)} aria-label="Delete {row.SiteName}">
                <Trash2 class="w-4 h-4" />
              </button>
            </div>
          {:else}
            {row[column.key]}
          {/if}
        {/snippet}
      </DataTable>
    {/if}
  </div>
</div>

<Modal bind:open={modalOpen} title={modalMode === 'create' ? 'Add Site' : 'Edit Site'} size="md">
  <form class="space-y-4" onsubmit={(e) => { e.preventDefault(); handleSave(); }}>
    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
      <div class="form-control">
        <label class="label" for="siteCode">
          <span class="label-text text-sm font-medium">Site Code</span>
          <span class="label-text-alt text-xs text-error">*</span>
        </label>
        <div class="relative">
          <input
            id="siteCode"
            type="text"
            class="input input-sm input-bordered w-full pr-16"
            class:input-success={isSiteCodeValid}
            class:input-error={siteCodeLength > 0 && !isSiteCodeValid}
            bind:value={formData.SiteCode}
            placeholder="e.g., AB"
            maxlength="10"
            required
          />
          <div class="absolute right-2 top-1/2 -translate-y-1/2">
            <span class="badge badge-sm {siteCodeBadgeClass}">
              {#if isSiteCodeValid}
                <Check class="w-3 h-3 mr-1" />
              {/if}
              {siteCodeLength}/2
            </span>
          </div>
        </div>
        <span class="label-text-alt text-xs text-gray-500">
          Must be exactly 2 characters (e.g., AB, NY, 01)
        </span>
      </div>
      <div class="form-control">
        <label class="label" for="siteName">
          <span class="label-text text-sm font-medium">Site Name</span>
          <span class="label-text-alt text-xs text-error">*</span>
        </label>
        <input
          id="siteName"
          type="text"
          class="input input-sm input-bordered w-full"
          bind:value={formData.SiteName}
          placeholder="e.g., Main Site"
          required
        />
        <span class="label-text-alt text-xs text-gray-500">Display name for this site</span>
      </div>
    </div>
    <div class="form-control">
      <label class="label" for="account">
        <span class="label-text text-sm font-medium">Account</span>
      </label>
      <select
        id="account"
        class="select select-sm select-bordered w-full"
        bind:value={formData.AccountID}
      >
        <option value={null}>Select account...</option>
        {#each accounts as account (account.id || account.AccountID)}
          <option value={account.id}>{account.AccountName}</option>
        {/each}
      </select>
      <span class="label-text-alt text-xs text-gray-500">The account this site belongs to</span>
    </div>
  </form>
  {#snippet footer()}
    <button class="btn btn-ghost" onclick={() => (modalOpen = false)} type="button">Cancel</button>
    <button class="btn btn-primary" onclick={handleSave} disabled={saving} type="button">
      {#if saving}
        <span class="loading loading-spinner loading-sm mr-2"></span>
      {/if}
      {saving ? 'Saving...' : 'Save'}
    </button>
  {/snippet}
</Modal>

<Modal bind:open={deleteConfirmOpen} title="Confirm Delete" size="sm">
  <div class="py-2">
    <p class="text-base-content/80">
      Are you sure you want to delete the following site?
    </p>
    <div class="bg-base-200 rounded-lg p-3 mt-3">
      <p class="font-semibold text-base-content">{deleteItem?.SiteName}</p>
      <p class="text-sm text-base-content/60">Code: {deleteItem?.SiteCode}</p>
    </div>
    <p class="text-sm text-error mt-4">This action cannot be undone.</p>
  </div>
  {#snippet footer()}
    <button class="btn btn-ghost" onclick={() => (deleteConfirmOpen = false)} disabled={deleting} type="button">
      Cancel
    </button>
    <button class="btn btn-error" onclick={handleDelete} disabled={deleting} type="button">
      {#if deleting}
        <span class="loading loading-spinner loading-sm mr-2"></span>
      {/if}
      {deleting ? 'Deleting...' : 'Delete'}
    </button>
  {/snippet}
</Modal>
