<?php

namespace App\Http\Controllers;

use App\Domain\Shared\Models\Academy;
use App\Domain\Website\Enums\SectionKey;
use App\Domain\Website\Models\WebsiteSetting;
use App\Domain\Website\Services\WebsiteDataService;
use App\Domain\Website\Services\WebsiteSectionService;
use App\Domain\Website\Services\WebsiteSettingService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class PublicWebsiteController extends Controller
{
    public function __construct(
        private WebsiteDataService $dataService,
        private WebsiteSectionService $sectionService,
        private WebsiteSettingService $settingService,
    ) {}

    public function home()
    {
        $academy = Academy::first();

        if (!$academy) {
            return redirect()->route('login');
        }

        $settings = $this->settingService->getOrCreate($academy);
        app()->instance('current_academy', $academy);
        app()->instance('website_settings', $settings);

        $sections = $this->sectionService->getEnabled($academy);
        $data = $this->gatherSectionData($academy, $sections);

        return view('website.index', [
            'academy' => $academy,
            'settings' => $settings,
            'sections' => $sections,
            'data' => $data,
            'showLoginButton' => true,
        ]);
    }

    public function show(string $slug)
    {
        $academy = app('current_academy');
        $settings = app('website_settings');

        $sections = $this->sectionService->getEnabled($academy);

        $data = $this->gatherSectionData($academy, $sections);

        return view('website.index', [
            'academy' => $academy,
            'settings' => $settings,
            'sections' => $sections,
            'data' => $data,
            'showLoginButton' => true,
        ]);
    }

    public function preview(Request $request)
    {
        $academy = app('current_academy');
        $settings = app(WebsiteSettingService::class)->getOrCreate($academy);

        app()->instance('website_settings', $settings);

        $sections = $this->sectionService->getAll($academy)->where('is_enabled', true);
        $data = $this->gatherSectionData($academy, $sections);

        return view('website.index', [
            'academy' => $academy,
            'settings' => $settings,
            'sections' => $sections,
            'data' => $data,
            'isPreview' => true,
        ]);
    }

    private function gatherSectionData(Academy $academy, $sections): array
    {
        $data = [];
        $enabledKeys = $sections->pluck('section_key');

        if ($enabledKeys->contains(SectionKey::Activities)) {
            $data['activities'] = $this->dataService->getActivities($academy);
        }

        if ($enabledKeys->contains(SectionKey::Programs)) {
            $data['programs'] = $this->dataService->getPrograms($academy);
        }

        if ($enabledKeys->contains(SectionKey::Branches)) {
            $data['branches'] = $this->dataService->getBranches($academy);
        }

        if ($enabledKeys->contains(SectionKey::Stats)) {
            $data['stats'] = $this->dataService->getStats($academy);
        }

        if ($enabledKeys->contains(SectionKey::Testimonials)) {
            $data['testimonials'] = $this->dataService->getTestimonials($academy);
        }

        if ($enabledKeys->contains(SectionKey::Faq)) {
            $data['faqs'] = $this->dataService->getFaqs($academy);
        }

        if ($enabledKeys->contains(SectionKey::News)) {
            $data['news'] = $this->dataService->getNews($academy);
        }

        if ($enabledKeys->contains(SectionKey::Partners)) {
            $data['partners'] = $this->dataService->getPartners($academy);
        }

        if ($enabledKeys->contains(SectionKey::Gallery)) {
            $data['gallery'] = $this->dataService->getGalleryImages($academy);
        }

        return $data;
    }
}
