<script>
  import { onMount } from 'svelte';
  import { fetchLocations, createLocation, updateLocation, deleteLocation } from '$lib/api/locations.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 SelectDropdown from '$lib/components/SelectDropdown.svelte';
  import HelpTooltip from '$lib/components/HelpTooltip.svelte';
  import { Plus, Edit2, Trash2, ArrowLeft, Search, MapPin } from 'lucide-svelte';

  let loading = $state(false);
  let locations = $state([]);
  let modalOpen = $state(false);
  let modalMode = $state('create');
  let saving = $state(false);
  let formData = $state({ LocationID: null, Code: '', Name: '', Type: '', ParentID: null });
  let deleteConfirmOpen = $state(false);
  let deleteItem = $state(null);
  let deleting = $state(false);
  let searchQuery = $state('');

  const typeLabels = {
    ROOM: 'Room',
    BUILDING: 'Building',
    FLOOR: 'Floor',
    AREA: 'Area',
  };

  const typeBadgeColors = {
    ROOM: 'badge-info',
    BUILDING: 'badge-success',
    FLOOR: 'badge-warning',
    AREA: 'badge-secondary',
  };

  const columns = [
    { key: 'LocCode', label: 'Code', class: 'font-medium' },
    { key: 'LocFull', label: 'Name' },
    { key: 'LocTypeLabel', label: 'Type' },
    { key: 'actions', label: 'Actions', class: 'w-32 text-center' },
  ];

  onMount(async () => {
    await loadLocations();
  });

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

  const filteredLocations = $derived(
    locations.filter((l) => {
      if (!searchQuery.trim()) return true;
      const query = searchQuery.toLowerCase();
      return (
        (l.LocCode && l.LocCode.toLowerCase().includes(query)) ||
        (l.LocFull && l.LocFull.toLowerCase().includes(query))
      );
    })
  );

  function openCreateModal() {
    modalMode = 'create';
    formData = { LocationID: null, Code: '', Name: '', Type: '', ParentID: null };
    modalOpen = true;
  }

  function openEditModal(row) {
    modalMode = 'edit';
    formData = {
      LocationID: row.LocationID,
      Code: row.LocCode,
      Name: row.LocFull,
      Type: row.LocType || '',
      ParentID: row.Parent,
    };
    modalOpen = true;
  }

  function isDuplicateCode(code, excludeId = null) {
    return locations.some(
      (l) =>
        l.LocCode.toLowerCase() === code.toLowerCase() &&
        l.LocationID !== excludeId
    );
  }

  async function handleSave() {
    if (!formData.Code.trim()) {
      toastError('Location code is required');
      return;
    }
    if (!formData.Name.trim()) {
      toastError('Location name is required');
      return;
    }
    if (!formData.Type) {
      toastError('Location type is required');
      return;
    }

    const excludeId = modalMode === 'edit' ? formData.LocationID : null;
    if (isDuplicateCode(formData.Code, excludeId)) {
      toastError(`Location code "${formData.Code}" already exists. Please use a unique code.`);
      return;
    }

    saving = true;
    try {
      if (modalMode === 'create') {
        await createLocation(formData);
        toastSuccess('Location created successfully');
      } else {
        await updateLocation(formData.LocationID, formData);
        toastSuccess('Location updated successfully');
      }
      modalOpen = false;
      await loadLocations();
    } catch (err) {
      toastError(err.message || 'Failed to save location');
    } finally {
      saving = false;
    }
  }

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

  async function handleDelete() {
    deleting = true;
    try {
      await deleteLocation(deleteItem.LocationID);
      toastSuccess('Location deleted successfully');
      deleteConfirmOpen = false;
      deleteItem = null;
      await loadLocations();
    } catch (err) {
      toastError(err.message || 'Failed to delete location');
    } finally {
      deleting = false;
    }
  }

  const parentOptions = $derived(
    locations
      .filter((l) => l.LocationID !== formData.LocationID)
      .map((l) => ({ value: l.LocationID.toString(), label: `${l.LocCode} - ${l.LocFull}` }))
  );

  function getTypeBadgeClass(type) {
    return typeBadgeColors[type] || 'badge-ghost';
  }
</script>

<div class="p-4">
  <div class="flex items-center gap-4 mb-6">
    <a href="/master-data" 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">Locations</h1>
      <p class="text-sm text-gray-600">Manage locations and facilities</p>
    </div>
    <button class="btn btn-primary" onclick={openCreateModal}>
      <Plus class="w-4 h-4 mr-2" />
      Add Location
    </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 && filteredLocations.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">
          <MapPin class="w-8 h-8 text-gray-400" />
        </div>
        <h3 class="text-base font-semibold text-base-content mb-1">
          {searchQuery ? 'No locations found' : 'No locations yet'}
        </h3>
        <p class="text-sm text-base-content/60 text-center max-w-sm mb-4">
          {searchQuery
            ? `No locations match your search "${searchQuery}". Try a different search term.`
            : 'Get started by adding your first location to organize your facilities.'}
        </p>
        {#if !searchQuery}
          <button class="btn btn-primary btn-sm" onclick={openCreateModal}>
            <Plus class="w-4 h-4 mr-2" />
            Add Your First Location
          </button>
        {/if}
      </div>
    {:else}
      <DataTable
        {columns}
        data={filteredLocations.map((l) => ({
          ...l,
          LocTypeLabel: typeLabels[l.LocType] || l.LocType || '-',
        }))}
        {loading}
        emptyMessage="No locations found"
        hover={true}
        bordered={false}
      >
        {#snippet cell({ column, row, value })}
          {#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.LocFull}">
                <Edit2 class="w-4 h-4" />
              </button>
              <button class="btn btn-sm btn-ghost text-error" onclick={() => confirmDelete(row)} aria-label="Delete {row.LocFull}">
                <Trash2 class="w-4 h-4" />
              </button>
            </div>
          {:else if column.key === 'LocTypeLabel'}
            <span class="badge {getTypeBadgeClass(row.LocType)}">
              {value}
            </span>
          {:else}
            {value}
          {/if}
        {/snippet}
      </DataTable>
    {/if}
  </div>
</div>

<Modal bind:open={modalOpen} title={modalMode === 'create' ? 'Add Location' : 'Edit Location'} size="md">
  <form class="space-y-3" 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="code">
          <span class="label-text text-sm font-medium">Location Code</span>
          <span class="label-text-alt text-xs text-error">*</span>
        </label>
        <input
          id="code"
          type="text"
          class="input input-sm input-bordered w-full"
          bind:value={formData.Code}
          placeholder="e.g., BLDG-01, ROOM-101"
          required
        />
        <label class="label" for="code">
          <span class="label-text-alt text-xs text-gray-500">Unique identifier for this location</span>
        </label>
      </div>
      <div class="form-control">
        <label class="label" for="name">
          <span class="label-text text-sm font-medium">Location Name</span>
          <span class="label-text-alt text-xs text-error">*</span>
        </label>
        <input
          id="name"
          type="text"
          class="input input-sm input-bordered w-full"
          bind:value={formData.Name}
          placeholder="e.g., Main Building, Laboratory Room A"
          required
        />
        <label class="label" for="name">
          <span class="label-text-alt text-xs text-gray-500">Descriptive name for this location</span>
        </label>
      </div>
    </div>
    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
      <div class="form-control">
        <label class="label" for="type">
          <span class="label-text text-sm font-medium">Location Type</span>
          <span class="label-text-alt text-xs text-error">*</span>
        </label>
        <select
          id="type"
          name="type"
          class="select select-sm select-bordered w-full"
          bind:value={formData.Type}
          required
        >
          <option value="">Select type...</option>
          <option value="ROOM">Room</option>
          <option value="BUILDING">Building</option>
          <option value="FLOOR">Floor</option>
          <option value="AREA">Area</option>
        </select>
        <label class="label" for="type">
          <span class="label-text-alt text-xs text-gray-500">Category of this location</span>
        </label>
      </div>
      <div class="form-control">
        <label class="label" for="parent">
          <span class="label-text text-sm font-medium">Parent Location</span>
          <HelpTooltip
            text="Select a parent location to create a hierarchy. For example, a Room can be inside a Building, or a Floor can be part of a Building."
            title="Location Hierarchy"
            position="top"
          />
        </label>
        <SelectDropdown
          name="parent"
          bind:value={formData.ParentID}
          options={parentOptions}
          placeholder="None (top-level location)"
        />
        <label class="label" for="parent">
          <span class="label-text-alt text-xs text-gray-500">Optional: parent location in hierarchy</span>
        </label>
      </div>
    </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 location?
    </p>
    <div class="bg-base-200 rounded-lg p-3 mt-3">
      <p class="font-semibold text-base-content">{deleteItem?.LocFull}</p>
      <p class="text-sm text-base-content/60">Code: {deleteItem?.LocCode}</p>
      {#if deleteItem?.LocType}
        <span class="badge badge-sm {getTypeBadgeClass(deleteItem.LocType)} mt-2">
          {typeLabels[deleteItem.LocType]}
        </span>
      {/if}
    </div>
    <p class="text-sm text-error mt-4">This action cannot be undone. Any child locations will become top-level locations.</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>
