<!-- [DEF:DashboardGrid:Component] -->
<!--
@SEMANTICS: dashboard, grid, selection, pagination
@PURPOSE:   Displays a grid of dashboards with selection and pagination.
@LAYER:     Component
@RELATION:  USED_BY -> frontend/src/routes/migration/+page.svelte

@INVARIANT: Selected IDs must be a subset of available dashboards.
-->

<script lang="ts">
  // [SECTION: IMPORTS]
  import { createEventDispatcher } from 'svelte';
  import type { DashboardMetadata } from '../types/dashboard';
  import { t } from '../lib/i18n';
  import { Button, Input } from '../lib/ui';
  import GitManager from './git/GitManager.svelte';
  // [/SECTION]

  // [SECTION: PROPS]
  export let dashboards: DashboardMetadata[] = [];
  export let selectedIds: number[] = [];
  // [/SECTION]

  // [SECTION: STATE]
  let filterText = "";
  let currentPage = 0;
  let pageSize = 20;
  let sortColumn: keyof DashboardMetadata = "title";
  let sortDirection: "asc" | "desc" = "asc";
  // [/SECTION]

  // [SECTION: UI STATE]
  let showGitManager = false;
  let gitDashboardId: number | null = null;
  let gitDashboardTitle = "";
  // [/SECTION]

  // [SECTION: DERIVED]
  $: filteredDashboards = dashboards.filter(d =>
    d.title.toLowerCase().includes(filterText.toLowerCase())
  );

  $: sortedDashboards = [...filteredDashboards].sort((a, b) => {
    let aVal = a[sortColumn];
    let bVal = b[sortColumn];
    if (sortColumn === "id") {
      aVal = Number(aVal);
      bVal = Number(bVal);
    }
    if (aVal < bVal) return sortDirection === "asc" ? -1 : 1;
    if (aVal > bVal) return sortDirection === "asc" ? 1 : -1;
    return 0;
  });

  $: paginatedDashboards = sortedDashboards.slice(
    currentPage * pageSize,
    (currentPage + 1) * pageSize
  );

  $: totalPages = Math.ceil(sortedDashboards.length / pageSize);

  $: allSelected = paginatedDashboards.length > 0 && paginatedDashboards.every(d => selectedIds.includes(d.id));
  $: someSelected = paginatedDashboards.some(d => selectedIds.includes(d.id));
  // [/SECTION]

  // [SECTION: EVENTS]
  const dispatch = createEventDispatcher<{ selectionChanged: number[] }>();
  // [/SECTION]

  // [DEF:handleSort:Function]
  // @PURPOSE: Toggles sort direction or changes sort column.
  // @PRE: column name is provided.
  // @POST: sortColumn and sortDirection state updated.
  function handleSort(column: keyof DashboardMetadata) {
    if (sortColumn === column) {
      sortDirection = sortDirection === "asc" ? "desc" : "asc";
    } else {
      sortColumn = column;
      sortDirection = "asc";
    }
  }
  // [/DEF:handleSort:Function]

  // [DEF:handleSelectionChange:Function]
  // @PURPOSE: Handles individual checkbox changes.
  // @PRE: dashboard ID and checked status provided.
  // @POST: selectedIds array updated and selectionChanged event dispatched.
  function handleSelectionChange(id: number, checked: boolean) {
    let newSelected = [...selectedIds];
    if (checked) {
      if (!newSelected.includes(id)) newSelected.push(id);
    } else {
      newSelected = newSelected.filter(sid => sid !== id);
    }
    selectedIds = newSelected;
    dispatch('selectionChanged', newSelected);
  }
  // [/DEF:handleSelectionChange:Function]

  // [DEF:handleSelectAll:Function]
  // @PURPOSE: Handles select all checkbox.
  // @PRE: checked status provided.
  // @POST: selectedIds array updated for all paginated items and event dispatched.
  function handleSelectAll(checked: boolean) {
    let newSelected = [...selectedIds];
    if (checked) {
      paginatedDashboards.forEach(d => {
        if (!newSelected.includes(d.id)) newSelected.push(d.id);
      });
    } else {
      paginatedDashboards.forEach(d => {
        newSelected = newSelected.filter(sid => sid !== d.id);
      });
    }
    selectedIds = newSelected;
    dispatch('selectionChanged', newSelected);
  }
  // [/DEF:handleSelectAll:Function]

  // [DEF:goToPage:Function]
  // @PURPOSE: Changes current page.
  // @PRE: page index is provided.
  // @POST: currentPage state updated if within valid range.
  function goToPage(page: number) {
    if (page >= 0 && page < totalPages) {
      currentPage = page;
    }
  }
  // [/DEF:goToPage:Function]

  // [DEF:openGit:Function]
  /**
   * @purpose Opens the Git management modal for a dashboard.
   */
  function openGit(dashboard: DashboardMetadata) {
    gitDashboardId = dashboard.id;
    gitDashboardTitle = dashboard.title;
    showGitManager = true;
  }
  // [/DEF:openGit:Function]

</script>

<!-- [SECTION: TEMPLATE] -->
<div class="dashboard-grid">
  <!-- Filter Input -->
  <div class="mb-6">
    <Input
      bind:value={filterText}
      placeholder={$t.dashboard.search}
    />
  </div>

  <!-- Grid/Table -->
  <div class="overflow-x-auto rounded-lg border border-gray-200">
    <table class="min-w-full divide-y divide-gray-200">
      <thead class="bg-gray-50">
        <tr>
          <th class="px-6 py-3 text-left">
            <input
              type="checkbox"
              checked={allSelected}
              indeterminate={someSelected && !allSelected}
              on:change={(e) => handleSelectAll((e.target as HTMLInputElement).checked)}
              class="h-4 w-4 text-blue-600 border-gray-300 rounded focus:ring-blue-500"
            />
          </th>
          <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider cursor-pointer hover:text-gray-700 transition-colors" on:click={() => handleSort('title')}>
            {$t.dashboard.title} {sortColumn === 'title' ? (sortDirection === 'asc' ? '↑' : '↓') : ''}
          </th>
          <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider cursor-pointer hover:text-gray-700 transition-colors" on:click={() => handleSort('last_modified')}>
            {$t.dashboard.last_modified} {sortColumn === 'last_modified' ? (sortDirection === 'asc' ? '↑' : '↓') : ''}
          </th>
          <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider cursor-pointer hover:text-gray-700 transition-colors" on:click={() => handleSort('status')}>
            {$t.dashboard.status} {sortColumn === 'status' ? (sortDirection === 'asc' ? '↑' : '↓') : ''}
          </th>
          <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">{$t.dashboard.git}</th>
        </tr>
      </thead>
      <tbody class="bg-white divide-y divide-gray-200">
        {#each paginatedDashboards as dashboard (dashboard.id)}
          <tr class="hover:bg-gray-50 transition-colors">
            <td class="px-6 py-4 whitespace-nowrap">
              <input
                type="checkbox"
                checked={selectedIds.includes(dashboard.id)}
                on:change={(e) => handleSelectionChange(dashboard.id, (e.target as HTMLInputElement).checked)}
                class="h-4 w-4 text-blue-600 border-gray-300 rounded focus:ring-blue-500"
              />
            </td>
            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">{dashboard.title}</td>
            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{new Date(dashboard.last_modified).toLocaleDateString()}</td>
            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
              <span class="px-2 py-1 text-xs font-medium rounded-full {dashboard.status === 'published' ? 'bg-green-100 text-green-800' : 'bg-gray-100 text-gray-800'}">
                {dashboard.status}
              </span>
            </td>
            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
              <Button
                variant="ghost"
                size="sm"
                on:click={() => openGit(dashboard)}
                class="text-blue-600 hover:text-blue-900"
              >
                {$t.git.manage}
              </Button>
            </td>
          </tr>
        {/each}
      </tbody>
    </table>
  </div>

  <!-- Pagination Controls -->
  <div class="flex items-center justify-between mt-6">
    <div class="text-sm text-gray-500">
      {($t.dashboard?.showing || "")
        .replace('{start}', (currentPage * pageSize + 1).toString())
        .replace('{end}', Math.min((currentPage + 1) * pageSize, sortedDashboards.length).toString())
        .replace('{total}', sortedDashboards.length.toString())}
    </div>
    <div class="flex gap-2">
      <Button
        variant="secondary"
        size="sm"
        disabled={currentPage === 0}
        on:click={() => goToPage(currentPage - 1)}
      >
        {$t.dashboard.previous}
      </Button>
      <Button
        variant="secondary"
        size="sm"
        disabled={currentPage >= totalPages - 1}
        on:click={() => goToPage(currentPage + 1)}
      >
        {$t.dashboard.next}
      </Button>
    </div>
  </div>
</div>

{#if showGitManager && gitDashboardId}
  <GitManager
    dashboardId={gitDashboardId}
    dashboardTitle={gitDashboardTitle}
    bind:show={showGitManager}
  />
{/if}

<!-- [/SECTION] -->

<style>
  /* Component styles */
</style>

<!-- [/DEF:DashboardGrid:Component] -->