<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class DocumentResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        $status = $this->effectiveStatus();
        $user = $request->user();

        return [
            'id' => $this->uuid,
            'document_type' => $this->whenLoaded('documentType', fn () => [
                'id' => $this->documentType->uuid,
                'code' => $this->documentType->code,
                'name' => $this->documentType->name,
                'category' => $this->documentType->category->value,
                'category_label' => $this->documentType->category->label(),
                'is_sensitive' => (bool) $this->documentType->is_sensitive,
            ]),

            'status' => $status->value,
            'status_label' => $status->label(),
            'is_satisfied' => $this->isSatisfied(),
            'awaits_review' => $status->awaitsReview(),

            'original_name' => $this->original_name,
            'mime_type' => $this->mime_type,
            'size_bytes' => $this->size_bytes,
            'version' => $this->version,
            'is_current' => (bool) $this->is_current,

            'document_number' => $this->document_number,
            'issue_date' => $this->issue_date?->toDateString(),
            'expiry_date' => $this->expiry_date?->toDateString(),
            'has_expired' => $this->hasExpired(),
            'expiring_soon' => $this->expiresWithin(30),
            'issuing_country' => $this->issuing_country,
            'issuer' => $this->issuer,

            'rejection_reason' => $this->rejection_reason,
            'uploaded_at' => $this->created_at?->toIso8601String(),
            'uploaded_by' => $this->whenLoaded('uploadedBy', fn () => $this->uploadedBy?->name),
            'verified_at' => $this->verified_at?->toIso8601String(),
            'verified_by' => $this->whenLoaded('verifiedBy', fn () => $this->verifiedBy?->name),

            // Staff-only commentary is never shown to applicants (§17B.9).
            'internal_notes' => $this->when(
                $user?->can('document.verify') ?? false,
                fn () => $this->internal_notes
            ),

            // What this reader is actually allowed to do with it.
            'can' => [
                'download' => $user?->can('download', $this->resource) ?? false,
                'verify' => $user?->can('verify', $this->resource) ?? false,
                'reject' => $user?->can('reject', $this->resource) ?? false,
            ],

            'owner' => $this->whenLoaded('owner', fn () => $this->owner ? [
                'type' => class_basename($this->owner_type),
                'id' => $this->owner->uuid ?? null,
                'name' => $this->owner->full_name ?? $this->owner->name ?? $this->owner->company_name ?? null,
                'reference' => $this->owner->reference_no ?? null,
            ] : null),
        ];
    }
}
