<?php

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

use App\Domain\Recruitment\Models\JobDemand;
use App\Domain\Website\Models\CompanyCertificate;
use App\Domain\Website\Models\DestinationCountry;
use App\Domain\Website\Models\JobCategory;
use App\Domain\Website\Models\SuccessStory;
use App\Domain\Website\Services\SiteContentService;
use App\Http\Controllers\Controller;
use App\Http\Resources\Public\PublicJobResource;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;

/**
 * Everything the homepage needs, in one request (§159).
 */
class PublicHomeController extends Controller
{
    public function __construct(private readonly SiteContentService $content) {}

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

        if ($company === null) {
            return ApiResponse::success(['configured' => false]);
        }

        return ApiResponse::success([
            'company' => [
                'name' => $company->name,
                'logo_url' => $company->logo_path ? url('storage/'.$company->logo_path) : null,
                'email' => $company->email,
                'phone' => $company->phone,
                'address' => $company->address,
                'license_number' => $company->license_number,
            ],
            'hero' => $this->content->group('hero'),
            'about' => $this->content->group('about'),
            'statistics' => $this->content->statistics(),

            'featured_jobs' => PublicJobResource::collection(
                JobDemand::publiclyVisible()
                    ->with(['employer:id,company_name', 'jobCategory:id,slug,name'])
                    ->orderByDesc('is_urgent')
                    ->orderByDesc('is_featured')
                    ->latest('publish_at')
                    ->limit(6)
                    ->get()
            ),

            'featured_countries' => DestinationCountry::published()
                ->where('company_id', $company->id)
                ->orderByDesc('is_featured')
                ->orderBy('sort_order')
                ->limit(8)
                ->get()
                ->map(fn (DestinationCountry $country) => [
                    'slug' => $country->slug,
                    'name' => $country->name,
                    'code' => $country->code,
                    'flag' => $country->flag_emoji,
                    'summary' => $country->summary,
                    // A photo when the agency has uploaded one; the site draws
                    // its own artwork when they have not.
                    'image_url' => $country->image_path ? url('storage/'.$country->image_path) : null,
                    'open_jobs' => JobDemand::publiclyVisible()->where('country', $country->code)->count(),
                ]),

            'categories' => JobCategory::published()
                ->where('company_id', $company->id)
                ->orderByDesc('is_featured')
                ->orderBy('sort_order')
                ->limit(8)
                ->get()
                ->map(fn (JobCategory $category) => [
                    'slug' => $category->slug,
                    'name' => $category->name,
                    'icon' => $category->icon,
                    'image_url' => $category->image_path ? url('storage/'.$category->image_path) : null,
                    'open_jobs' => JobDemand::publiclyVisible()->where('job_category_id', $category->id)->count(),
                ]),

            'success_stories' => SuccessStory::published()
                ->where('company_id', $company->id)
                ->orderByDesc('is_featured')
                ->latest('published_on')
                ->limit(3)
                ->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,
                    'image_url' => $story->image_path ? url('storage/'.$story->image_path) : null,
                ]),

            'certificates' => CompanyCertificate::published()
                ->where('company_id', $company->id)
                ->orderBy('sort_order')
                ->get()
                ->map(fn (CompanyCertificate $certificate) => [
                    'name' => $certificate->name,
                    'issuing_authority' => $certificate->issuing_authority,
                    'image_url' => $certificate->image_path ? url('storage/'.$certificate->image_path) : null,
                ]),
        ]);
    }
}
