<?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)
    {
        // The marketing homepage is reachable at `/` for everyone — guests AND
        // logged-in users (who get a "Dashboard" button in the nav to jump
        // back to the app). We intentionally do NOT bounce authed visitors to
        // their dashboard, so the landing page is always one click away.
        // Login itself still lands users on /home → dashboard via its own
        // redirect; this only governs an explicit visit to `/`.
        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);
    }
}
