<script lang="ts">
  import { appRpc } from './rpc.ts';
  import { t } from './i18n.svelte.ts';

  interface Props {
    onSelect: (path: string) => void;
    onClose: () => void;
  }

  let { onSelect, onClose }: Props = $props();

  let HOME = $state('/home');
  let shortcuts = $state([
    { label: 'Home', path: '~' },
    { label: '/tmp', path: '/tmp' },
  ]);

  // Load XDG user directories from backend
  async function loadXdgDirs() {
    try {
      const r = await appRpc.request['files.homeDir']({});
      if (r?.path) HOME = r.path;
      // Check which XDG dirs actually exist
      const candidates = [
        { label: 'Desktop', env: 'XDG_DESKTOP_DIR', fallback: `${HOME}/Desktop` },
        { label: 'Documents', env: 'XDG_DOCUMENTS_DIR', fallback: `${HOME}/Documents` },
        { label: 'Downloads', env: 'XDG_DOWNLOAD_DIR', fallback: `${HOME}/Downloads` },
        { label: 'Projects', env: 'XDG_PROJECTS_DIR', fallback: `${HOME}/Projects` },
      ];
      // Validate each via files.browse (only add if dir exists)
      const existing: typeof shortcuts = [{ label: 'Home', path: '~' }];
      for (const c of candidates) {
        const path = c.fallback.replace(HOME, '~');
        const res = await appRpc.request['files.browse']({ path: c.fallback }).catch(() => null);
        if (res && !res.error) existing.push({ label: c.label, path });
      }
      existing.push({ label: '/tmp', path: '/tmp' });
      shortcuts = existing;
    } catch {}
  }

  let currentPath = $state('~');
  let entries = $state<Array<{ name: string; type: 'file' | 'dir'; size: number }>>([]);
  let loading = $state(false);
  let filter = $state('');
  let error = $state('');

  // Breadcrumb segments
  let breadcrumbs = $derived(() => {
    const parts = currentPath.split('/').filter(Boolean);
    const crumbs: Array<{ label: string; path: string }> = [];
    let acc = '';
    for (const part of parts) {
      acc += (acc === '' && currentPath.startsWith('~') && crumbs.length === 0) ? part : '/' + part;
      if (crumbs.length === 0 && currentPath.startsWith('~')) acc = part;
      crumbs.push({ label: part, path: acc.startsWith('~') ? acc : '/' + acc });
    }
    return crumbs;
  });

  let filteredEntries = $derived(
    entries.filter(e => e.type === 'dir' && (filter === '' || e.name.toLowerCase().includes(filter.toLowerCase())))
  );

  // Load home dir from backend on first use
  async function resolveHome(): Promise<string> {
    if (HOME !== '/home') return HOME;
    try {
      const r = await appRpc.request['files.homeDir']({});
      if (r?.path) HOME = r.path;
    } catch {}
    return HOME;
  }

  async function loadDir(dirPath: string) {
    loading = true;
    error = '';
    try {
      const home = await resolveHome();
      const expandedPath = dirPath.replace(/^~/, home);
      const result = await appRpc.request['files.browse']({ path: expandedPath });
      if (result?.error) {
        error = result.error;
        entries = [];
      } else {
        entries = result?.entries ?? [];
      }
      currentPath = dirPath;
    } catch (err) {
      const raw = err instanceof Error ? err.message : String(err);
      // Sanitize: show user-friendly message, not raw ENOENT
      if (raw.includes('ENOENT') || raw.includes('no such file')) {
        error = 'Directory not found';
      } else if (raw.includes('EACCES') || raw.includes('permission')) {
        error = 'Permission denied';
      } else {
        error = 'Cannot access this directory';
      }
      entries = [];
    } finally {
      loading = false;
    }
  }

  function navigateTo(dirPath: string) {
    filter = '';
    loadDir(dirPath);
  }

  function selectFolder(name: string) {
    const sep = currentPath.endsWith('/') ? '' : '/';
    const newPath = currentPath + sep + name;
    navigateTo(newPath);
  }

  function confirmSelect() {
    onSelect(currentPath);
  }

  // Load initial directory
  $effect(() => {
    loadXdgDirs();
    loadDir('~');
  });
</script>

<div class="path-browser">
  <!-- Header with Select always visible -->
  <div class="pb-header">
    <span class="pb-title">{t('wizard.step1.browse' as any)}</span>
    <div class="pb-header-actions">
      <span class="pb-current-compact" title={currentPath}>{currentPath}</span>
      <button class="pb-select-btn" onclick={confirmSelect}>Select</button>
      <button class="pb-close" onclick={onClose} aria-label={t('common.close' as any)}>✕</button>
    </div>
  </div>

  <!-- Shortcuts -->
  <div class="pb-shortcuts">
    {#each shortcuts as sc}
      <button class="pb-shortcut" onclick={() => navigateTo(sc.path)}>{sc.label}</button>
    {/each}
  </div>

  <!-- Breadcrumbs -->
  <div class="pb-breadcrumbs">
    {#each breadcrumbs() as crumb, i}
      {#if i > 0}<span class="pb-sep">/</span>{/if}
      <button class="pb-crumb" onclick={() => navigateTo(crumb.path)}>{crumb.label}</button>
    {/each}
  </div>

  <!-- Filter -->
  <input
    class="pb-filter"
    type="text"
    placeholder="Filter..."
    bind:value={filter}
  />

  <!-- Directory list -->
  <div class="pb-list">
    {#if loading}
      <div class="pb-loading">Loading...</div>
    {:else if error}
      <div class="pb-error">{error}</div>
    {:else if filteredEntries.length === 0}
      <div class="pb-empty">No directories</div>
    {:else}
      {#each filteredEntries as entry}
        <button class="pb-entry" onclick={() => selectFolder(entry.name)}>
          <span class="pb-icon">📁</span>
          <span class="pb-name">{entry.name}</span>
        </button>
      {/each}
    {/if}
  </div>

  <!-- Footer: current path (Select moved to header) -->
  <div class="pb-footer">
    <span class="pb-current" title={currentPath}>{currentPath}</span>
  </div>
</div>

<style>
  .path-browser {
    position: relative;
    z-index: 10;
    background: var(--ctp-mantle);
    border: 1px solid var(--ctp-surface1);
    border-radius: 0.375rem;
    display: flex;
    flex-direction: column;
    max-height: 18rem;
    max-width: 100%;
    box-shadow: 0 0.5rem 1.5rem color-mix(in srgb, var(--ctp-crust) 60%, transparent);
  }

  .pb-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.375rem 0.5rem;
    border-bottom: 1px solid var(--ctp-surface0);
    position: sticky;
    top: 0;
    z-index: 2;
    background: var(--ctp-mantle);
  }
  .pb-header-actions {
    display: flex;
    align-items: center;
    gap: 0.375rem;
  }
  .pb-current-compact {
    font-size: 0.6875rem;
    color: var(--ctp-subtext0);
    max-width: 12rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    direction: rtl;
    text-align: left;
  }

  .pb-title { font-size: 0.75rem; font-weight: 600; color: var(--ctp-text); }

  .pb-close {
    background: none; border: none; color: var(--ctp-overlay1); cursor: pointer;
    font-size: 0.75rem; padding: 0.125rem 0.25rem; border-radius: 0.25rem;
  }
  .pb-close:hover { color: var(--ctp-text); background: var(--ctp-surface0); }

  .pb-shortcuts {
    display: flex; gap: 0.25rem; padding: 0.25rem 0.5rem;
    border-bottom: 1px solid var(--ctp-surface0);
  }

  .pb-shortcut {
    padding: 0.125rem 0.375rem; font-size: 0.625rem; border-radius: 0.25rem;
    background: var(--ctp-surface0); border: none; color: var(--ctp-subtext0);
    cursor: pointer; font-family: var(--ui-font-family);
  }
  .pb-shortcut:hover { background: var(--ctp-surface1); color: var(--ctp-text); }

  .pb-breadcrumbs {
    display: flex; align-items: center; gap: 0.125rem;
    padding: 0.25rem 0.5rem; flex-wrap: wrap;
    font-size: 0.6875rem; color: var(--ctp-subtext0);
  }

  .pb-sep { color: var(--ctp-overlay0); }

  .pb-crumb {
    background: none; border: none; color: var(--ctp-blue); cursor: pointer;
    font-size: 0.6875rem; padding: 0; font-family: var(--ui-font-family);
  }
  .pb-crumb:hover { text-decoration: underline; }

  .pb-filter {
    margin: 0.25rem 0.5rem; padding: 0.25rem 0.375rem;
    background: var(--ctp-surface0); border: 1px solid var(--ctp-surface1);
    border-radius: 0.25rem; color: var(--ctp-text);
    font-size: 0.75rem; font-family: var(--ui-font-family);
  }
  .pb-filter:focus { outline: none; border-color: var(--ctp-blue); }
  .pb-filter::placeholder { color: var(--ctp-overlay0); }

  .pb-list {
    flex: 1; min-height: 0; overflow-y: auto;
    padding: 0.25rem 0;
  }

  .pb-list::-webkit-scrollbar { width: 0.25rem; }
  .pb-list::-webkit-scrollbar-thumb { background: var(--ctp-surface1); border-radius: 0.125rem; }

  .pb-entry {
    display: flex; align-items: center; gap: 0.375rem;
    width: 100%; padding: 0.25rem 0.5rem;
    background: none; border: none; color: var(--ctp-text);
    cursor: pointer; font-size: 0.75rem; text-align: left;
    font-family: var(--ui-font-family);
  }
  .pb-entry:hover { background: var(--ctp-surface0); }

  .pb-icon { font-size: 0.875rem; flex-shrink: 0; }
  .pb-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  .pb-loading, .pb-error, .pb-empty {
    padding: 1rem; text-align: center; font-size: 0.75rem;
    color: var(--ctp-overlay0);
  }
  .pb-error { color: var(--ctp-red); }

  .pb-footer {
    display: flex; align-items: center; justify-content: space-between;
    padding: 0.375rem 0.5rem; border-top: 1px solid var(--ctp-surface0);
    gap: 0.5rem;
  }

  .pb-current {
    font-size: 0.6875rem; color: var(--ctp-subtext0);
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
    flex: 1; min-width: 0;
  }

  .pb-select-btn {
    padding: 0.25rem 0.625rem; font-size: 0.75rem; border-radius: 0.25rem;
    background: color-mix(in srgb, var(--ctp-blue) 20%, transparent);
    border: 1px solid var(--ctp-blue); color: var(--ctp-blue);
    cursor: pointer; font-family: var(--ui-font-family); flex-shrink: 0;
  }
  .pb-select-btn:hover { background: color-mix(in srgb, var(--ctp-blue) 35%, transparent); }
</style>
