---
import Base from '../../layouts/Base.astro';
import { getPublishedProducts, getProductBySlug, urlFor } from '../../lib/sanity.mjs';

const { slug } = Astro.params;
const product = await getProductBySlug(slug);

if (!product) {
    return Astro.redirect('/');
}

const name = product.name;
const title = product.seoTitle || `REBOURS — ${product.productDisplayName} | Collection 001`;
const description = product.seoDescription || product.description?.substring(0, 155) || '';
const ogImage = product.images?.[0] ? urlFor(product.images[0]).width(1200).url() : '';
const productName = product.productDisplayName;
const price = product.price ? String(product.price / 100) : null;
const availability = product.availability || 'https://schema.org/PreOrder';

const allProducts = await getPublishedProducts();

const schemaProduct = {
    "@context": "https://schema.org",
    "@type": "Product",
    "name": productName,
    "description": description,
    "image": ogImage,
    "brand": { "@type": "Brand", "name": "REBOURS Studio" },
    "url": `https://rebours.studio/collection/${slug}/`,
    ...(price ? {
        "offers": {
            "@type": "Offer",
            "price": price,
            "priceCurrency": "EUR",
            "availability": availability,
            "seller": { "@type": "Organization", "name": "REBOURS Studio" }
        }
    } : {}),
};

const schemaBreadcrumb = {
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    "itemListElement": [
        { "@type": "ListItem", "position": 1, "name": "Accueil", "item": "https://rebours.studio/" },
        { "@type": "ListItem", "position": 2, "name": "Collection 001", "item": "https://rebours.studio/#collection" },
        { "@type": "ListItem", "position": 3, "name": productName, "item": `https://rebours.studio/collection/${slug}/` },
    ]
};
---

<Base
    title={title}
    description={description}
    ogImage={ogImage}
    canonical={`https://rebours.studio/collection/${slug}/`}
>
    <Fragment slot="head">
        <script type="application/ld+json" set:html={JSON.stringify(schemaProduct)} />
        <script type="application/ld+json" set:html={JSON.stringify(schemaBreadcrumb)} />
    </Fragment>

    <meta name="x-open-panel" content={name} />

    <script is:inline>
        window.__OPEN_PANEL__ = document.querySelector('meta[name="x-open-panel"]')?.content;
    </script>

    <!-- Grid background -->
    <div id="interactive-grid" class="interactive-grid"></div>

    <!-- PRODUCT PANEL (overlay) -->
    <div id="product-panel" class="product-panel" aria-hidden="true">
        <div class="panel-close" id="panel-close">
            <span>← RETOUR</span>
        </div>
        <div class="panel-inner">
            <div class="panel-img-col">
                <div class="panel-gallery" id="panel-gallery">
                    <img id="panel-img" src="" alt="Image produit REBOURS Studio">
                </div>
                <div class="panel-gallery-nav" id="panel-gallery-nav"></div>
            </div>
            <div class="panel-info-col">
                <p class="panel-index" id="panel-index"></p>
                <h2 id="panel-name"></h2>
                <hr>
                <div class="panel-meta">
                    <div class="panel-meta-row">
                        <span class="meta-key">TYPE</span>
                        <span id="panel-type"></span>
                    </div>
                    <div class="panel-meta-row">
                        <span class="meta-key">MATÉRIAUX</span>
                        <span id="panel-mat"></span>
                    </div>
                    <div class="panel-meta-row">
                        <span class="meta-key">ANNÉE</span>
                        <span id="panel-year"></span>
                    </div>
                    <div class="panel-meta-row">
                        <span class="meta-key">STATUS</span>
                        <span id="panel-status" class="red"></span>
                    </div>
                </div>
                <hr>
                <p id="panel-desc" class="panel-desc"></p>
                <hr>

                <details class="accordion" open>
                    <summary>SPÉCIFICATIONS TECHNIQUES <span>↓</span></summary>
                    <div class="accordion-body" id="panel-specs"></div>
                </details>
                <details class="accordion" open>
                    <summary>NOTES DE CONCEPTION <span>↓</span></summary>
                    <div class="accordion-body" id="panel-notes"></div>
                </details>

                <hr>

                <div id="checkout-section" style="display:none;">
                    <div class="checkout-price-line">
                        <span class="checkout-price"></span>
                        <span class="checkout-edition">ÉDITION UNIQUE — 1/1</span>
                    </div>
                    <button id="checkout-toggle-btn" class="checkout-btn">
                        [ COMMANDER CETTE PIÈCE ]
                    </button>
                    <div id="checkout-form-wrap" class="checkout-form-wrap" style="display:none;">
                        <form id="checkout-form" class="checkout-form">
                            <div class="checkout-form-field">
                                <label for="checkout-email">EMAIL *</label>
                                <input type="email" id="checkout-email" name="email" placeholder="votre@email.com" required autocomplete="off">
                            </div>
                            <div class="checkout-form-note">
                                Pièce fabriquée à Paris. Délai : 6 à 8 semaines.<br>
                                Paiement sécurisé via Stripe.
                            </div>
                            <button type="submit" class="checkout-submit" id="checkout-submit-btn">
                                PROCÉDER AU PAIEMENT →
                            </button>
                        </form>
                    </div>
                </div>

                <div class="panel-footer">
                    <span class="blink">■</span> COLLECTION_001 — W.I.P
                </div>
            </div>
        </div>
    </div>

    <div class="page-wrapper">

        <header class="header">
            <a href="/" class="logo-text" aria-label="REBOURS — Accueil">REBOURS</a>
            <nav class="header-nav" aria-label="Navigation principale">
                <a href="/#collection">COLLECTION_001</a>
                <a href="#" class="contact-trigger">CONTACT</a>
                <span class="wip-tag"><span class="blink">■</span> W.I.P</span>
            </nav>
        </header>

        <main>

            <section class="hero" aria-label="Introduction">
                <div class="hero-left">
                    <p class="label">// ARCHIVE_001 — 2026</p>
                    <h1>REBOURS<br>STUDIO</h1>
                    <p class="hero-sub">Mobilier d'art contemporain.<br>Space Age × Memphis.</p>
                    <p class="hero-sub mono-sm">STATUS: [PROTOTYPE EN COURS]<br>COLLECTION_001 — BIENTÔT DISPONIBLE</p>
                </div>
                <div class="hero-right">
                    {allProducts[0]?.images?.[0] && (
                        <img
                            src={urlFor(allProducts[0].images[0]).width(1024).url()}
                            alt="REBOURS — Mobilier d'art contemporain, Paris 2026"
                            class="hero-img"
                            width="1024" height="1024"
                            fetchpriority="high">
                    )}
                </div>
            </section>

            <hr>

            <section class="collection" id="collection" aria-label="Collection 001">
                <div class="collection-header">
                    <p class="label">// COLLECTION_001</p>
                    <span class="label">{allProducts.length} OBJETS — CLIQUER POUR OUVRIR</span>
                </div>

                <div class="product-grid">
                    {allProducts.map((p, i) => {
                        const mainImg = p.images?.[0];
                        const imgUrl = mainImg ? urlFor(mainImg).width(800).url() : '';
                        const alt = p.imageAlt || `${p.productDisplayName} — mobilier d'art contemporain, REBOURS Studio Paris`;
                        const allImgs = (p.images || []).map(img => ({
                            url: urlFor(img).width(1200).url(),
                            alt: img.alt || alt,
                        }));
                        return (
                            <article class="product-card"
                                data-index={p.index}
                                data-name={p.name}
                                data-type={p.type}
                                data-mat={p.materials}
                                data-year={p.year}
                                data-status={p.status}
                                data-desc={p.description}
                                data-specs={p.specs || ''}
                                data-notes={p.notes || ''}
                                data-img={mainImg ? urlFor(mainImg).width(1200).url() : ''}
                                data-images={JSON.stringify(allImgs)}
                                data-price={p.price ? String(p.price) : ''}
                                data-slug={p.slug}
                                data-img-alt={alt}
                                aria-label={`Ouvrir le détail de ${p.productDisplayName}`}>
                                <div class="card-img-wrap">
                                    <img src={imgUrl}
                                         alt={alt}
                                         width="600" height="600"
                                         loading={i === 0 ? "eager" : "lazy"}>
                                </div>
                                <div class="card-meta">
                                    <span class="card-index">{String(i + 1).padStart(3, '0')}</span>
                                    <span class="card-name">{p.name}</span>
                                    <span class="card-arrow">↗</span>
                                </div>
                            </article>
                        );
                    })}
                </div>
            </section>

            <section class="newsletter" id="contact" aria-label="Accès anticipé">
                <div class="nl-left">
                    <p class="label">// ACCÈS_ANTICIPÉ</p>
                    <h2>REJOINDRE<br>L'EXPÉRIENCE</h2>
                </div>
                <div class="nl-right">
                    <form class="nl-form" onsubmit="event.preventDefault();" aria-label="Inscription newsletter">
                        <label for="nl-email">EMAIL :</label>
                        <div class="nl-row">
                            <input type="email" id="nl-email" name="email" placeholder="votre@email.com" autocomplete="email" required>
                            <button type="submit">ENVOYER →</button>
                        </div>
                        <p class="mono-sm"><span class="blink">■</span> CONNECTION_STATUS: PENDING</p>
                    </form>
                </div>
            </section>

        </main>

        <footer class="footer">
            <span>© 2026 REBOURS STUDIO — PARIS</span>
            <nav aria-label="Liens secondaires">
                <a href="https://instagram.com/rebour.studio" rel="noopener" target="_blank">INSTAGRAM</a>
                &nbsp;/&nbsp;
                <a href="#" class="contact-trigger">CONTACT</a>
            </nav>
        </footer>

    </div>

    <!-- CONTACT MODAL -->
    <div id="contact-modal" class="contact-modal" aria-hidden="true">
        <div class="contact-modal-backdrop"></div>
        <div class="contact-modal-content">
            <div class="contact-modal-header">
                <p class="label">// CONTACT</p>
                <button class="contact-modal-close" aria-label="Fermer">✕</button>
            </div>
            <hr>
            <h2 class="contact-modal-title">ENVOYER<br>UN MESSAGE</h2>
            <form id="contact-form" class="contact-form">
                <div class="contact-field">
                    <label for="contact-name">NOM *</label>
                    <input type="text" id="contact-name" name="name" placeholder="Votre nom" required autocomplete="name">
                </div>
                <div class="contact-field">
                    <label for="contact-email">EMAIL *</label>
                    <input type="email" id="contact-email" name="email" placeholder="votre@email.com" required autocomplete="email">
                </div>
                <div class="contact-field">
                    <label for="contact-subject">OBJET</label>
                    <input type="text" id="contact-subject" name="subject" placeholder="Objet du message" autocomplete="off">
                </div>
                <div class="contact-field">
                    <label for="contact-message">MESSAGE *</label>
                    <textarea id="contact-message" name="message" rows="5" placeholder="Votre message..." required></textarea>
                </div>
                <button type="submit" class="contact-submit">
                    ENVOYER LE MESSAGE →
                </button>
                <p class="mono-sm contact-note"><span class="blink">■</span> REDIRECTION VERS VOTRE CLIENT MAIL</p>
            </form>
        </div>
    </div>

    <div class="cursor-dot"></div>
    <div class="cursor-outline"></div>
</Base>
