<script>
  import { getUser, getUserProfile, setHomeCountry } from './userStore.svelte.js';
  import { countryNames } from '../shared/countries.js';
  import homeImg from '../../assets 2/home.png';

  let user = $derived(getUser());
  let profile = $derived(getUserProfile());

  let search = $state('');
  let selectedCountry = $state('');
  let open = $state(false);

  let filtered = $derived(
    search.trim()
      ? countryNames.filter(c => c.toLowerCase().includes(search.toLowerCase()))
      : countryNames
  );

  function select(name) {
    selectedCountry = name;
    search = name;
    open = false;
  }

  function handleSubmit() {
    if (selectedCountry) {
      setHomeCountry(selectedCountry, selectedCountry);
    }
  }

  function handleKeydown(e) {
    if (e.key === 'Enter' && selectedCountry) handleSubmit();
    if (e.key === 'Escape') open = false;
  }
</script>

<div class="overlay">
  <div class="card">
    <img src={homeImg} alt="home" class="home-img" />
    <h1 class="title">Welcome, {profile?.displayName?.split(' ')[0] || 'Traveler'}!</h1>
    <p class="subtitle">Where do you call home?</p>

    <div class="dropdown">
      <input
        type="text"
        placeholder="Search country..."
        bind:value={search}
        onfocus={() => { open = true; }}
        oninput={() => { open = true; selectedCountry = ''; }}
        onkeydown={handleKeydown}
        class="search-input"
      />
      {#if open && filtered.length > 0}
        <ul class="list" role="listbox">
          {#each filtered as name}
            <li
              role="option"
              aria-selected={selectedCountry === name}
              class:selected={selectedCountry === name}
              onmousedown={() => select(name)}
              tabindex="0"
            >{name}</li>
          {/each}
        </ul>
      {/if}
    </div>

    <button class="continue-btn" disabled={!selectedCountry} onclick={handleSubmit}>
      Set home country
    </button>
  </div>
</div>

<style>
  .overlay {
    position: fixed;
    inset: 0;
    background: var(--bg);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    padding-bottom: 20vh;
  }

  .card {
    text-align: center;
    max-width: 360px;
    width: 90%;
    display: flex;
    flex-direction: column;
    align-items: center;
  }

  .home-img {
    width: 200px;
    height: 200px;
    object-fit: contain;
    margin-bottom: 16px;
  }

  .title {
    font-family: var(--heading);
    font-size: 24px;
    font-weight: 600;
    color: var(--text-h);
    letter-spacing: -0.5px;
    margin: 0 0 6px;
  }

  .subtitle {
    font-family: var(--sans);
    font-size: 14px;
    font-weight: 300;
    color: var(--text);
    margin: 0 0 24px;
  }

  .dropdown {
    position: relative;
    width: 100%;
    margin-bottom: 16px;
    text-align: left;
  }

  .search-input {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg-subtle);
    color: var(--text-h);
    font-family: var(--sans);
    font-size: 14px;
    font-weight: 300;
    outline: none;
    transition: border-color 0.15s;
    box-sizing: border-box;
  }
  .search-input:focus { border-color: var(--accent-border); }
  .search-input::placeholder { color: var(--text-sub); }

  .list {
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    max-height: 220px;
    overflow-y: auto;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    list-style: none;
    z-index: 10;
    padding: 4px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.1);
  }

  .list li {
    padding: 8px 12px;
    cursor: pointer;
    color: var(--text);
    font-family: var(--sans);
    font-size: 13px;
    font-weight: 300;
    border-radius: 6px;
    transition: background 0.1s;
  }

  .list li:hover, .list li.selected {
    background: var(--accent-bg);
    color: var(--accent);
  }

  .continue-btn {
    width: 100%;
    padding: 11px 24px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--accent);
    color: #fff;
    font-family: var(--sans);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s, opacity 0.15s;
  }
  .continue-btn:hover:not(:disabled) { background: var(--accent-dark); }
  .continue-btn:disabled { opacity: 0.4; cursor: default; }
</style>
