---
import path from "node:path";

import DefaultLayout from "../../layouts/DefaultLayout.astro";
import SectionRenderer from "../../components/sections/SectionRenderer.astro";
import JamboxCards from "../../islands/jambox/JamboxCards.jsx";
import SectionChannelsSearch from "../../components/sections/SectionChannelsSearch.astro";

import { loadYamlFile } from "../../lib/loadYaml";

type SeoYaml = any;

type Param = { klucz: string; label: string; value: string | number };
type Cena = {
  budynek: number | string;
  umowa: number | string;
  miesiecznie: number;
  aktywacja?: number;
};
type Card = {
  id?: string;
  source?: string;
  tid?: number;
  nazwa: string;
  slug?: string;
  widoczny?: boolean;
  popularny?: boolean;
  parametry?: Param[];
  ceny?: Cena[];
};

type CardsYaml = {
  tytul?: string;
  opis?: string;
  waluta?: string;
  cena_opis?: string;
  internet_parametry_wspolne?: Param[];
  cards?: Card[];
};

// ✅ telefon z YAML (do modala)
type PhoneParam = { klucz: string; label: string; value: string | number };
type PhoneCard = {
  id?: string;
  nazwa: string;
  widoczny?: boolean;
  popularny?: boolean;
  cena?: { wartosc: number; opis?: string };
  parametry?: PhoneParam[];
};
type PhoneYaml = { cards?: PhoneCard[] };

type Decoder = { id: string; nazwa: string; opis: string; cena: number };

// ✅ dodatki z YAML (do modala)
type Addon = {
  id: string;
  nazwa: string;
  typ?: "checkbox" | "quantity";
  ilosc?: boolean;
  min?: number;
  max?: number;
  krok?: number;
  opis?: string;
  cena: number;
};
type AddonsYaml = {
  tytul?: string;
  opis?: string;
  cena_opis?: string;
  dekodery?: Decoder[];
  dodatki?: Addon[];
};

// type ChannelsYaml = {
//   title?: string;
//   updated_at?: string;
//   channels?: Array<{
//     nazwa: string;
//     opis?: string;
//     image?: string;
//     pakiety?: string[];
//   }>;
// };

const seo = loadYamlFile<SeoYaml>(
  path.join(process.cwd(), "src", "content", "internet-telewizja", "seo.yaml"),
);

const data = loadYamlFile<CardsYaml>(
  path.join(
    process.cwd(),
    "src",
    "content",
    "internet-telewizja",
    "cards.yaml",
  ),
);

const tytul = data?.tytul ?? "";
const opis = data?.opis ?? "Wybierz rodzaj budynku i czas trwania umowy";

const waluta = data?.waluta ?? "PLN";
const cenaOpis = data?.cena_opis ?? "zł/mies.";

const internetWspolne: Param[] = Array.isArray(data?.internet_parametry_wspolne)
  ? data.internet_parametry_wspolne
  : [];

const cards: Card[] = Array.isArray(data?.cards)
  ? data.cards.filter((c) => c?.widoczny === true)
  : [];

// ✅ NOWE: dane do modala dodatków (bez ruszania reszty)
const phoneYaml = loadYamlFile<PhoneYaml>(
  path.join(process.cwd(), "src", "content", "telefon", "cards.yaml"),
);

const tvAddonsYaml = loadYamlFile<any>(
  path.join(process.cwd(), "src", "content", "internet-telewizja", "tv-addons.yaml"),
);

const phoneCards = Array.isArray(phoneYaml?.cards) ? phoneYaml.cards : [];
const tvAddons = Array.isArray(tvAddonsYaml?.dodatki) ? tvAddonsYaml.dodatki : [];

const addonsYaml = loadYamlFile<AddonsYaml>(
  path.join(
    process.cwd(),
    "src",
    "content",
    "internet-telewizja",
    "addons.yaml",
  ),
);
const addons: Addon[] = Array.isArray(addonsYaml?.dodatki)
  ? addonsYaml.dodatki
  : [];

const decoders: Decoder[] = Array.isArray(addonsYaml?.dekodery)
  ? addonsYaml.dekodery
  : [];

const addonsCenaOpis = addonsYaml?.cena_opis ?? cenaOpis;

type SwitchOption = { id: string | number; nazwa: string };
type SwitchDef = {
  id: string;
  etykieta?: string;
  title?: string;
  domyslny?: string | number;
  opcje: SwitchOption[];
};
type SwitchesYaml = { switches?: SwitchDef[] };

const switchesYaml = loadYamlFile<SwitchesYaml>(
  path.join(process.cwd(), "src", "content", "site", "switches.yaml"),
);

const switches: SwitchDef[] = Array.isArray(switchesYaml?.switches)
  ? switchesYaml.switches
  : [];

---

<DefaultLayout seo={seo}>
  <section class="f-section">
    <div class="f-section-grid-single md:grid-cols-1">
      <JamboxCards
        client:load
        title={tytul}
        description={opis}
        cards={cards}
        internetWspolne={internetWspolne}
        waluta={waluta}
        cenaOpis={cenaOpis}
        tvAddons={tvAddons}
        phoneCards={phoneCards}
        decoders={decoders}
        addons={addons}
        addonsCenaOpis={addonsCenaOpis}
        switches={switches}
      />
    </div>
  </section>
  <SectionChannelsSearch />
  <SectionRenderer src="./src/content/internet-telewizja/section.yaml" />
</DefaultLayout>
