<?php

namespace App\Http\Controllers\Api\V1\PublicSite;

use App\Domain\Website\Models\CompanyCertificate;
use App\Domain\Website\Models\DestinationCountry;
use App\Domain\Website\Models\JobCategory;
use App\Domain\Website\Models\SiteFaq;
use App\Domain\Website\Models\SuccessStory;
use App\Domain\Website\Services\SiteContentService;
use App\Http\Controllers\Controller;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

/** Public content pages: about, countries, categories, stories, FAQs (§160, §170–§173). */
class PublicContentController extends Controller
{
    public function __construct(private readonly SiteContentService $content) {}

    public function about(): JsonResponse
    {
        $company = $this->content->company();

        return ApiResponse::success([
            'company' => $company === null ? null : [
                'name' => $company->name,
                'legal_name' => $company->legal_name,
                'license_number' => $company->license_number,
                'registration_number' => $company->registration_number,
                'address' => $company->address,
                'email' => $company->email,
                'phone' => $company->phone,
                'website' => $company->website,
            ],
            'content' => $this->content->group('about'),
            'process' => $this->content->group('process'),
            'statistics' => $this->content->statistics(),
            'certificates' => CompanyCertificate::published()
                ->orderBy('sort_order')
                ->get()
                ->map(fn (CompanyCertificate $certificate) => [
                    'name' => $certificate->name,
                    'issuing_authority' => $certificate->issuing_authority,
                    'certificate_number' => $certificate->certificate_number,
                    'issue_date' => $certificate->issue_date?->toDateString(),
                    'expiry_date' => $certificate->expiry_date?->toDateString(),
                    'description' => $certificate->description,
                    'image_url' => $certificate->image_path ? url('storage/'.$certificate->image_path) : null,
                ]),
        ]);
    }

    public function countries(): JsonResponse
    {
        return ApiResponse::success(
            DestinationCountry::published()
                ->orderByDesc('is_featured')
                ->orderBy('sort_order')
                ->get()
                ->map(fn (DestinationCountry $country) => [
                    'slug' => $country->slug,
                    'code' => $country->code,
                    'name' => $country->name,
                    'flag' => $country->flag_emoji,
                    'summary' => $country->summary,
                    'description' => $country->description,
                    'currency' => $country->currency,
                    'image_url' => $country->image_path ? url('storage/'.$country->image_path) : null,
                ])
        );
    }

    public function categories(): JsonResponse
    {
        return ApiResponse::success(
            JobCategory::published()
                ->orderByDesc('is_featured')
                ->orderBy('sort_order')
                ->get()
                ->map(fn (JobCategory $category) => [
                    'slug' => $category->slug,
                    'name' => $category->name,
                    'description' => $category->description,
                    'icon' => $category->icon,
                    'image_url' => $category->image_path ? url('storage/'.$category->image_path) : null,
                ])
        );
    }

    public function stories(): JsonResponse
    {
        return ApiResponse::success(
            SuccessStory::published()
                ->orderByDesc('is_featured')
                ->latest('published_on')
                ->get()
                ->map(fn (SuccessStory $story) => [
                    'slug' => $story->slug,
                    'title' => $story->title,
                    'worker_name' => $story->worker_name,
                    'occupation' => $story->occupation,
                    'country' => $story->country,
                    'excerpt' => $story->excerpt,
                    'body' => $story->body,
                    'published_on' => $story->published_on?->toDateString(),
                    'image_url' => $story->image_path ? url('storage/'.$story->image_path) : null,
                ])
        );
    }

    public function faqs(Request $request): JsonResponse
    {
        return ApiResponse::success(
            SiteFaq::published()
                ->when($request->filled('audience'), fn ($q) => $q->where('audience', $request->string('audience')))
                ->orderBy('sort_order')
                ->get(['question', 'answer', 'audience'])
        );
    }

    public function contactDetails(): JsonResponse
    {
        $company = $this->content->company();

        return ApiResponse::success([
            'company' => $company === null ? null : [
                'name' => $company->name,
                'email' => $company->email,
                'phone' => $company->phone,
                'address' => $company->address,
            ],
            'content' => $this->content->group('contact'),
            'branches' => $company === null ? [] : $company->branches()
                ->where('is_active', true)
                ->get(['name', 'address', 'city', 'phone', 'email'])
                ->map(fn ($branch) => [
                    'name' => $branch->name,
                    'address' => $branch->address,
                    'city' => $branch->city,
                    'phone' => $branch->phone,
                    'email' => $branch->email,
                ]),
        ]);
    }
}
