<?php

declare(strict_types=1);

namespace App\Http\Resources;

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

class ProducerResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        $id = $this->id;

        return [
            'id' => $id,
            'slug' => Str::slug($this->brand_name ?? '') ?: "seller-{$id}",
            'user_id' => $this->user_id,
            'brand' => $this->brand_name,
            'brand_name' => $this->brand_name,
            'bio' => $this->bio,
            'fullname' => $this->brand_name ?? $this->whenLoaded('user', fn () => $this->user?->name),
            'username' => $this->whenLoaded('user', fn () => $this->user?->name),
            'work_email' => $this->work_email,
            'work_phone' => $this->work_phone,
            'activity_main_category' => $this->activity_main_category,
            'activity_sub_category' => $this->activity_sub_category,
            'activity_product' => $this->activity_product,
            'unit_capacity' => $this->unit_capacity,
            'unit_area' => $this->unit_area !== null ? (float) $this->unit_area : null,
            'work_province_id' => $this->work_province_id,
            'work_county_id' => $this->work_county_id,
            'work_province' => $this->whenLoaded('province', fn () => $this->province?->only(['id', 'name'])),
            'work_county' => $this->whenLoaded('county', fn () => $this->county?->only(['id', 'name'])),
            '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),
            'certificates' => [
                'education' => $this->formatFiles($this->education_certificates) ?? [],
                'skill' => $this->formatFiles($this->skill_certificates) ?? [],
                'business_license' => $this->formatFiles($this->business_license) ?? [],
            ],
            'work_experience' => $this->work_experience,
            'ownership_document' => FileUrl::url($this->ownership_document),
            'lease_start_date' => $this->lease_start_date?->toDateString(),
            'lease_end_date' => $this->lease_end_date?->toDateString(),
            'unit_images' => $this->formatFiles($this->unit_images),
            'unit_videos' => $this->formatFiles($this->unit_videos),
            'status' => $this->status,
            'status_label' => Producer::statusOptions()[$this->status] ?? $this->status,
            'admin_reason' => $this->admin_reason,
            'reviewed_at' => $this->reviewed_at?->toISOString(),
            'image' => $this->whenLoaded('user', fn () => FileUrl::url($this->user?->avatar)),
            'product_count' => $this->whenCounted('products'),
            'products_count' => $this->whenCounted('products'),
            'projects_count' => $this->whenCounted('projects'),
            'satisfaction_avg' => $this->satisfaction_avg !== null ? (float) $this->satisfaction_avg : null,
            'ratings' => $this->when(isset($this->ratings_list), fn () => $this->ratings_list),
            'active_ads' => $this->whenCounted('products'),
            'created_at' => $this->created_at?->toISOString(),
            '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'])),
        ];
    }

    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
        )));
    }
}
