<?php

declare(strict_types=1);

namespace App\Http\Resources;

use App\Models\Expert;
use App\Support\FileUrl;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class ExpertResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'user_id' => $this->user_id,
            'brand_name' => $this->brand_name,
            'display_name' => $this->display_name,
            'position' => $this->position,
            'workplace' => $this->workplace,
            'work_email' => $this->work_email,
            'work_phone' => $this->work_phone,
            'main_category' => $this->main_category,
            'sub_category' => $this->sub_category,
            'species_type' => $this->species_type,
            'products' => $this->products,
            'work_province_id' => $this->work_province_id,
            'work_county_id' => $this->work_county_id,
            'work_address' => $this->work_address,
            'work_postal_code' => $this->work_postal_code,
            'work_latitude' => $this->work_latitude !== null ? (float) $this->work_latitude : null,
            'work_longitude' => $this->work_longitude !== null ? (float) $this->work_longitude : null,
            'business_license' => $this->formatFiles($this->business_license),
            'education_certificates' => $this->formatFiles($this->education_certificates),
            'skill_certificates' => $this->formatFiles($this->skill_certificates),
            'work_experience' => $this->work_experience,
            'status' => $this->status,
            'status_label' => Expert::statusOptions()[$this->status] ?? $this->status,
            'admin_reason' => $this->admin_reason,
            'reviewed_at' => $this->reviewed_at?->toISOString(),
            'created_at' => $this->created_at?->toISOString(),
            'stats' => [
                'consultations_count' => (int) ($this->consultations_count ?? 0),
                'satisfaction_avg' => $this->satisfaction_avg !== null ? (float) $this->satisfaction_avg : null,
                'projects_count' => (int) ($this->projects_count
                    ?? (is_array($this->work_experience) ? count($this->work_experience) : 0)),
            ],
            'featured_projects' => $this->featured_projects ?? [],
            'user' => new UserResource($this->whenLoaded('user')),
            'province' => $this->whenLoaded('province', fn () => $this->province?->only(['id', 'name'])),
            'county' => $this->whenLoaded('county', fn () => $this->county?->only(['id', 'name'])),
            'education_items' => $this->whenLoaded('educationItems', function () {
                return $this->educationItems->map(fn ($item) => [
                    'id' => $item->id,
                    'title' => $item->title,
                    'institution' => $item->institution,
                    'year' => $item->year,
                    'document_url' => FileUrl::url($item->document_path),
                    'sort_order' => $item->sort_order,
                ])->values();
            }),
            'certificates' => $this->whenLoaded('certificates', function () {
                return $this->certificates->map(fn ($item) => [
                    'id' => $item->id,
                    'name' => $item->name,
                    'document_url' => FileUrl::url($item->document_path),
                    'verified' => (bool) $item->verified,
                    'verified_at' => $item->verified_at?->toISOString(),
                    'sort_order' => $item->sort_order,
                ])->values();
            }),
        ];
    }

    private function formatFiles(mixed $files): ?array
    {
        if ($files === null) {
            return null;
        }

        if (! is_array($files)) {
            return array_filter([is_string($files) ? FileUrl::url($files) : null]);
        }

        return array_values(array_filter(array_map(
            fn ($file) => is_string($file) ? FileUrl::url($file) : null,
            $files
        )));
    }
}
