{% load static %}
{# CAST SKY overlay — natal chart entry + D3 wheel preview           #}
{# Included in room.html when table_status == "SKY_SELECT"           #}
{# Opens when user clicks #id_pick_sky_btn; html.sky-open controls #}
{# visibility via CSS — backdrop-filter blur + centred modal.        #}

<div class="sky-backdrop"></div>
<div class="sky-overlay"
     id="id_sky_overlay"
     data-preview-url="{% url 'epic:sky_preview' room.id %}"
     data-save-url="{% url 'epic:sky_save' room.id %}"
     data-delete-url="{% url 'epic:sky_delete' room.id %}"
     data-sea-partial-url="{% url 'epic:sea_partial' room.id %}"
     data-user-seat-role="{{ user_seat_role }}">

    <div class="sky-modal-wrap">
    <div class="sky-modal">

        <header class="sky-modal-header">
            <h2>SKY <span>SELECT</span></h2>
            <p>Enter your birth details to generate your natal chart.</p>
        </header>

        <div class="sky-modal-body">

            {# ── Form column ──────────────────────────────────────── #}
            <div class="sky-form-col">

                {# form-main scrolls independently; confirm btn stays pinned below it #}
                <div class="sky-form-main">
                    <form id="id_sky_form" autocomplete="off">

                        <div class="sky-field">
                            <label for="id_nf_date">Birth date</label>
                            <input id="id_nf_date" name="date" type="date" required>
                        </div>

                        <div class="sky-field">
                            <label for="id_nf_time">Birth time</label>
                            <input id="id_nf_time" name="time" type="time" value="12:00">
                            <small>Local time at birth place. Use 12:00 if unknown.</small>
                        </div>

                        <div class="sky-field sky-place-field">
                            <label for="id_nf_place">Birth place</label>
                            <div class="sky-place-wrap">
                                <input id="id_nf_place" name="place" type="text"
                                       placeholder="Start typing a city…"
                                       autocomplete="off">
                                <button type="button" id="id_nf_geolocate"
                                        class="btn btn-secondary btn-sm"
                                        title="Use device location">
                                    <i class="fa-solid fa-location-crosshairs"></i>
                                </button>
                            </div>
                            <div id="id_nf_suggestions" class="sky-suggestions" hidden></div>
                        </div>

                        <div class="sky-field sky-coords">
                            <div>
                                <label>Latitude</label>
                                <input id="id_nf_lat" name="lat" type="text"
                                       placeholder="—" readonly tabindex="-1">
                            </div>
                            <div>
                                <label>Longitude</label>
                                <input id="id_nf_lon" name="lon" type="text"
                                       placeholder="—" readonly tabindex="-1">
                            </div>
                        </div>

                        <div class="sky-field">
                            <label for="id_nf_tz">Timezone</label>
                            <input id="id_nf_tz" name="tz" type="text"
                                   placeholder="auto-detected from coordinates"
                                   readonly tabindex="-1">
                        </div>

                    </form>

                    <div id="id_sky_status" class="sky-status"></div>
                </div>{# /.sky-form-main #}

                <button type="button" id="id_sky_confirm" class="btn btn-primary" disabled>
                    Save Sky
                </button>

            </div>

            {# ── Wheel column ─────────────────────────────────────── #}
            {# DEL btn is JS-injected after the wheel paints (see schedule  #}
            {# Preview success handler) — keeping it out of the template    #}
            {# means a blank CAST SKY modal can never show a DEL action     #}
            {# against a non-existent wheel.                                #}
            <div class="sky-wheel-col">
                <svg id="id_sky_svg" class="sky-svg"></svg>
            </div>

        </div>{# /.sky-modal-body #}

    </div>{# /.sky-modal #}

    {# NVM: circle btn centered on the top-right corner of the modal #}
    <button type="button" id="id_sky_cancel" class="btn btn-cancel btn-sm">NVM</button>

    </div>{# /.sky-modal-wrap #}
</div>{# /.sky-overlay #}

<script src="{% static 'apps/gameboard/d3.min.js' %}"></script>
<script src="{% static 'apps/gameboard/sky-wheel.js' %}"></script>
<script>
(function () {
  'use strict';

  const overlay      = document.getElementById('id_sky_overlay');
  const form         = document.getElementById('id_sky_form');
  const svgEl        = document.getElementById('id_sky_svg');
  const statusEl     = document.getElementById('id_sky_status');
  const confirmBtn   = document.getElementById('id_sky_confirm');
  const cancelBtn    = document.getElementById('id_sky_cancel');
  const geoBtn       = document.getElementById('id_nf_geolocate');
  const placeInput   = document.getElementById('id_nf_place');
  const latInput     = document.getElementById('id_nf_lat');
  const lonInput     = document.getElementById('id_nf_lon');
  const tzInput      = document.getElementById('id_nf_tz');
  const suggestions  = document.getElementById('id_nf_suggestions');

  const PREVIEW_URL  = overlay.dataset.previewUrl;
  const SAVE_URL     = overlay.dataset.saveUrl;
  const DELETE_URL   = overlay.dataset.deleteUrl;
  const NOMINATIM    = 'https://nominatim.openstreetmap.org/search';
  const USER_AGENT   = 'EarthmanRPG/1.0 (https://earthmanrpg.me)';

  let _lastChartData  = null;
  let _placeDebounce  = null;
  let _chartDebounce  = null;
  // Bumped on every clear so an in-flight schedulePreview() resolving after
  // DEL doesn't paint the wheel back & re-inject the DEL btn.
  let _fetchSeq       = 0;
  const PLACE_DELAY   = 400;   // ms — Nominatim polite rate
  const CHART_DELAY   = 300;   // ms — chart preview debounce

  // Preload zodiac SVG icons eagerly — they'll be cached before any draw() call.
  // To swap an icon, replace the .svg file in zodiac-signs/ and hard-refresh.
  SkyWheel.preload();

  // ── localStorage persistence ──────────────────────────────────────────────
  // Key scoped to room so multiple rooms don't clobber each other.

  const LS_KEY = 'sky-form:' + SAVE_URL;

  function _saveForm() {
    const data = {
      date:  document.getElementById('id_nf_date').value,
      time:  document.getElementById('id_nf_time').value,
      place: placeInput.value,
      lat:   latInput.value,
      lon:   lonInput.value,
      tz:    tzInput.value,
    };
    try { localStorage.setItem(LS_KEY, JSON.stringify(data)); } catch (_) {}
  }

  function _restoreForm() {
    let data;
    try { data = JSON.parse(localStorage.getItem(LS_KEY) || 'null'); } catch (_) {}
    if (!data) return;
    if (data.date)  document.getElementById('id_nf_date').value = data.date;
    if (data.time)  document.getElementById('id_nf_time').value = data.time;
    if (data.place) placeInput.value = data.place;
    if (data.lat)   latInput.value   = data.lat;
    if (data.lon)   lonInput.value   = data.lon;
    if (data.tz)    { tzInput.value  = data.tz; }
  }

  // ── Open / Close ──────────────────────────────────────────────────────────

  function openSky() {
    document.documentElement.classList.add('sky-open');
    // If the wheel is empty but the form has enough data (restored from
    // localStorage), kick off a fresh preview so the animation plays.
    if (!svgEl.querySelector('*') && _formReady()) {
      schedulePreview();
    }
  }

  function closeSky() {
    document.documentElement.classList.remove('sky-open');
    hideSuggestions();
  }

  const pickSkyBtn = document.getElementById('id_pick_sky_btn');
  if (pickSkyBtn) pickSkyBtn.addEventListener('click', openSky);
  cancelBtn.addEventListener('click', closeSky);
  overlay.addEventListener('click', (e) => { if (e.target === overlay) closeSky(); });

  // ── Status helper ─────────────────────────────────────────────────────────

  function setStatus(msg, type) {
    statusEl.textContent = msg;
    statusEl.className = 'sky-status' + (type ? ` sky-status--${type}` : '');
  }

  // ── Nominatim place search ────────────────────────────────────────────────

  placeInput.addEventListener('input', () => {
    clearTimeout(_placeDebounce);
    const q = placeInput.value.trim();
    if (q.length < 3) { hideSuggestions(); return; }
    _placeDebounce = setTimeout(() => fetchPlaces(q), PLACE_DELAY);
  });

  placeInput.addEventListener('keydown', (e) => {
    if (e.key === 'Escape') hideSuggestions();
  });

  document.addEventListener('click', (e) => {
    if (!placeInput.contains(e.target) && !suggestions.contains(e.target)) {
      hideSuggestions();
    }
  });

  function fetchPlaces(query) {
    fetch(`${NOMINATIM}?format=json&q=${encodeURIComponent(query)}&limit=6`, {
      headers: { 'User-Agent': USER_AGENT },
    })
      .then(r => r.json())
      .then(results => {
        if (!results.length) { hideSuggestions(); return; }
        renderSuggestions(results);
      })
      .catch(() => hideSuggestions());
  }

  function renderSuggestions(results) {
    suggestions.innerHTML = '';
    results.forEach(place => {
      const item = document.createElement('button');
      item.type = 'button';
      item.className = 'sky-suggestion-item';
      item.textContent = place.display_name;
      item.addEventListener('click', () => selectPlace(place));
      suggestions.appendChild(item);
    });
    suggestions.hidden = false;
  }

  function hideSuggestions() {
    suggestions.hidden = true;
    suggestions.innerHTML = '';
  }

  function selectPlace(place) {
    placeInput.value = place.display_name;
    latInput.value   = parseFloat(place.lat).toFixed(4);
    lonInput.value   = parseFloat(place.lon).toFixed(4);
    tzInput.value    = '';
    hideSuggestions();
    _saveForm();
    schedulePreview();
  }

  // ── Geolocation ───────────────────────────────────────────────────────────

  geoBtn.addEventListener('click', () => {
    if (!navigator.geolocation) {
      setStatus('Geolocation not supported by this browser.', 'error');
      return;
    }
    setStatus('Requesting device location…');
    navigator.geolocation.getCurrentPosition(
      (pos) => {
        latInput.value = pos.coords.latitude.toFixed(4);
        lonInput.value = pos.coords.longitude.toFixed(4);
        tzInput.value  = '';
        fetch(`https://nominatim.openstreetmap.org/reverse?format=json&lat=${latInput.value}&lon=${lonInput.value}`, {
          headers: { 'User-Agent': USER_AGENT },
        })
          .then(r => r.json())
          .then(data => { placeInput.value = _cityName(data.address) || data.display_name || ''; })
          .catch(() => {})
          .finally(() => _saveForm());
        setStatus('');
        schedulePreview();
      },
      () => setStatus('Location access denied.', 'error'),
    );
  });

  // Build a "City, State, Country" string from a Nominatim address object.
  // Prefers the most specific incorporated place name available.
  function _cityName(addr) {
    if (!addr) return '';
    const city    = addr.city || addr.town || addr.village || addr.hamlet || addr.municipality || '';
    const region  = addr.state || addr.county || addr.state_district || '';
    const country = addr.country || '';
    return [city, region, country].filter(Boolean).join(', ');
  }

  // ── Debounced chart preview ───────────────────────────────────────────────

  // Trigger on date / time / tz changes (coords come via selectPlace / geolocation)
  form.addEventListener('input', (e) => {
    if (e.target === placeInput) return;   // place triggers via selectPlace
    _saveForm();
    clearTimeout(_chartDebounce);
    _chartDebounce = setTimeout(schedulePreview, CHART_DELAY);
  });

  function _formReady() {
    return document.getElementById('id_nf_date').value &&
           latInput.value && lonInput.value;
  }

  function schedulePreview() {
    if (!_formReady()) return;
    const date = document.getElementById('id_nf_date').value;
    const time = document.getElementById('id_nf_time').value || '12:00';
    const lat  = latInput.value;
    const lon  = lonInput.value;
    const tz   = tzInput.value.trim();   // optional — proxy resolves if blank

    const params = new URLSearchParams({ date, time, lat, lon });
    if (tz) params.set('tz', tz);

    setStatus('Calculating…');
    confirmBtn.disabled = true;

    const seq = ++_fetchSeq;

    fetch(`${PREVIEW_URL}?${params}`, { credentials: 'same-origin' })
      .then(r => {
        if (!r.ok) throw new Error(`HTTP ${r.status}`);
        return r.json();
      })
      .then(data => {
        // Stale fetch — DEL ran (or another preview superseded). Bail before
        // we paint the wheel & re-inject the DEL btn against cleared state.
        if (seq !== _fetchSeq) return;

        _lastChartData = data;

        // Back-fill timezone field from proxy response (first render)
        if (!tzInput.value && data.timezone) {
          tzInput.value = data.timezone;
        }

        setStatus('');
        confirmBtn.disabled = false;
        if (svgEl.querySelector('*')) {
          SkyWheel.redraw(data);
        } else {
          SkyWheel.draw(svgEl, data);
        }
        _ensureDelBtn();
      })
      .catch(err => {
        if (seq !== _fetchSeq) return;
        setStatus(`Could not fetch chart: ${err.message}`, 'error');
        confirmBtn.disabled = true;
      });
  }

  // ── Save ──────────────────────────────────────────────────────────────────

  confirmBtn.addEventListener('click', () => {
    if (!_lastChartData) return;
    confirmBtn.disabled = true;
    setStatus('Saving…');

    const payload = {
      birth_dt:     `${document.getElementById('id_nf_date').value}T${document.getElementById('id_nf_time').value || '12:00'}:00`,
      birth_lat:    parseFloat(latInput.value),
      birth_lon:    parseFloat(lonInput.value),
      birth_place:  placeInput.value,
      house_system: _lastChartData.house_system || 'O',
      chart_data:   _lastChartData,
      action:       'confirm',
    };

    fetch(SAVE_URL, {
      method:      'POST',
      credentials: 'same-origin',
      headers: { 'Content-Type': 'application/json', 'X-CSRFToken': _getCsrf() },
      body: JSON.stringify(payload),
    })
      .then(r => {
        if (!r.ok) throw new Error(`HTTP ${r.status}`);
        return r.json();
      })
      .then(data => {
        if (!data.confirmed) {
          setStatus('Sky saved!');
        }
        // Confirmed state is driven by the room:sky_confirmed WS event
      })
      .catch(err => {
        setStatus(`Save failed: ${err.message}`, 'error');
        confirmBtn.disabled = false;
      });
  });

  // ── Sky confirmed → close sky & reload to land on hex w. DRAW SEA ──────
  //
  // The gamer should witness the table hex (now showing DRAW SEA in place of
  // CAST SKY) before opting into the sea overlay. We reload the room page —
  // the server-side template will re-render with `sky_confirmed=True` so the
  // hex's btn flips automatically, and the user clicks DRAW SEA to continue.

  function _onSkyConfirmed() {
    closeSky();
    window.location.reload();
  }

  // ── DEL btn — JS-injected after the wheel paints; absent on a blank modal
  // CAST SKY's wheel is a live preview; un-saved data lives only in LS_KEY.
  // The btn is created lazily after the first SkyWheel.draw so a blank modal
  // can never offer a DEL action against a non-existent wheel; clearing the
  // SVG removes the btn from the DOM entirely (re-injected on next preview).

  let _delBtn = null;
  function _ensureDelBtn() {
    if (_delBtn) return;
    const wheelCol = document.querySelector('.sky-wheel-col');
    if (!wheelCol) return;
    _delBtn = document.createElement('button');
    _delBtn.type = 'button';
    _delBtn.id = 'id_sky_delete_btn';
    _delBtn.className = 'btn btn-danger';
    _delBtn.textContent = 'DEL';
    wheelCol.appendChild(_delBtn);
    _delBtn.addEventListener('click', () => {
      if (!window.showGuard) return;
      window.showGuard(_delBtn, 'Forget sky?', () => {
        // 1. Invalidate any in-flight preview so its .then() can't paint the
        //    wheel back & re-inject this btn after we've cleared.
        _fetchSeq++;
        // 2. Cancel pending debounces so a typed-just-before-DEL keystroke
        //    can't fire schedulePreview after the clear.
        clearTimeout(_chartDebounce);
        clearTimeout(_placeDebounce);
        // 3. Server purge — drops any Character (draft or confirmed) on this
        //    seat, so a refresh after SAVE-then-DEL doesn't rehydrate state
        //    from the durable Character row.
        fetch(DELETE_URL, {
          method: 'POST',
          credentials: 'same-origin',
          headers: { 'X-CSRFToken': _getCsrf() },
        }).catch(() => {});
        // 4. Client DOM/state reset.
        while (svgEl.firstChild) svgEl.removeChild(svgEl.firstChild);
        form.reset();
        latInput.value = '';
        lonInput.value = '';
        tzInput.value  = '';
        _lastChartData = null;
        confirmBtn.disabled = true;
        setStatus('');
        try { localStorage.removeItem(LS_KEY); } catch (_) {}
        if (_delBtn) {
          _delBtn.remove();
          _delBtn = null;
        }
      });
    });
  }

  // ── CSRF ──────────────────────────────────────────────────────────────────

  function _getCsrf() {
    const m = document.cookie.match(/csrftoken=([^;]+)/);
    return m ? m[1] : '';
  }

  // ── Restore persisted form data ────────────────────────────────────────────
  // Called after all functions are defined. Wheel draw is deferred to
  // openSky() so the animation plays when the modal opens, not silently
  // in the background on page load.

  // WS: server broadcasts sky_confirmed when any gamer confirms their sky.
  // Only act when the event's seat_role matches this browser's seat.
  const MY_SEAT_ROLE = overlay.dataset.userSeatRole;

  window.addEventListener('room:sky_confirmed', function (e) {
    if (MY_SEAT_ROLE && e.detail.seat_role && e.detail.seat_role !== MY_SEAT_ROLE) return;
    _onSkyConfirmed();
  });

  _restoreForm();
})();
</script>
