<?php

    namespace App\Http\Controllers;

    use App\Models\Page;

    class LegalController extends Controller
    {
        /**
         * Resolve a legal/static page's content from the CMS, honoring the
         * admin's chosen slug AND falling back to common aliases so admins
         * who saved their Terms page as "terms-of-service" or "agreement"
         * still see it render on the public site.
         *
         * Previously these methods looked the row up by `type` — but
         * `pages.type` is an ENUM('landing','static','blog') and never
         * stored the semantic value ('terms', 'privacy', etc.), so the
         * frontend always rendered an empty page even though the admin
         * thought they'd saved fine.
         */
        private function content(string $slug): mixed
        {
            // Returns whatever the admin saved — could be a raw HTML
            // string (whole-page template) OR a structured array (with
            // subtitle / sections[] / etc.). The legal views render
            // both shapes — raw HTML in a prose wrapper, structured
            // arrays in the card grid.
            $aliases = Page::SLUG_ALIASES[$slug] ?? [];
            return Page::findRawContentBySlug($slug, $aliases);
        }

        public function terms()
        {
            $content = $this->content('terms');
            $siteName = config('app.name');
            return view('frontend.legal.terms', compact('content', 'siteName'));
        }

        public function privacy()
        {
            $content = $this->content('privacy');
            $siteName = config('app.name');
            return view('frontend.legal.privacy', compact('content', 'siteName'));
        }

        public function refund()
        {
            $content = $this->content('refund');
            $siteName = config('app.name');
            return view('frontend.legal.refund', compact('content', 'siteName'));
        }

        public function contact()
        {
            $content = $this->content('contact');
            return view('frontend.legal.contact-page', compact('content'));
        }

        public function about()
        {
            $content = $this->content('about');
            return view('frontend.legal.about', compact('content'));
        }

        public function whyUs()
        {
            $content = $this->content('why-us');
            return view('frontend.legal.why-us', compact('content'));
        }
    }
