<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

/**
 * Public marketing surface.
 *
 * Renders the homepage / features / pricing pages that live at /, /features,
 * /pricing. Authed visitors hitting `/` get redirected to /home which fans
 * out to dashboard or team-inbox based on role — the marketing page is
 * strictly for guests.
 *
 * Auth-dashboard pricing (live $packages, yearly toggle, wallet credits)
 * lives at /account/plans and is served by PricingController, not this
 * controller.
 */
class FrontendController extends Controller
{
    public function home(Request $request)
    {
        // Frontend kill-switch (admin → /admin/frontend toggle). When OFF, the
        // public HOMEPAGE redirects straight to login (guests) / the app
        // (authed) — but every OTHER page (privacy, terms, legal, pricing…)
        // keeps working. An admin previewing in the live editor (?fc_edit=1)
        // is exempt so the homepage stays editable.
        $editing = $request->boolean('fc_edit') && \Illuminate\Support\Facades\Auth::check();
        if (! (bool) \App\Models\SystemSetting::get('frontend_enabled', true) && ! $editing) {
            return \Illuminate\Support\Facades\Auth::check()
                ? redirect()->route('home')
                : redirect()->route('login');
        }

        return view('frontend.home');
    }

    public function features(Request $request)
    {
        return view('frontend.features');
    }

    public function pricing(Request $request)
    {
        return view('frontend.pricing');
    }

    public function about(Request $request)
    {
        return view('frontend.about');
    }

    public function contact(Request $request)
    {
        return view('frontend.contact');
    }

    /*
     * Legal pages — SaaS-CRM standard set. Content is fully admin-editable
     * via /admin/legal-pages (legal_pages table); the shared
     * <x-frontend.legal-page> component supplies the hero + TOC + chrome.
     */
    public function terms(Request $request)          { return $this->legal('terms'); }
    public function privacy(Request $request)        { return $this->legal('privacy'); }
    public function refund(Request $request)         { return $this->legal('refund'); }
    public function cookies(Request $request)        { return $this->legal('cookies'); }
    public function acceptableUse(Request $request)  { return $this->legal('acceptable-use'); }

    /**
     * Render a legal page from the DB. Hidden pages 404; if the row is
     * missing entirely (fresh install before the seeder ran) we fall back to
     * the original hardcoded blade so the page never breaks.
     */
    private function legal(string $slug)
    {
        $page = \App\Models\LegalPage::where('slug', $slug)->first();

        if ($page && ! $page->is_published) {
            abort(404);
        }
        if ($page) {
            return view('frontend.legal.dynamic', ['page' => $page]);
        }

        return view('frontend.legal.' . $slug);
    }
}
