<?php

namespace App\Console\Commands;

use App\Models\ClientCase;
use App\Models\Contract;
use App\Models\Document;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;

class DebugDocumentView extends Command
{
    protected $signature = 'debug:document {doc_uuid} {case_uuid}';

    protected $description = 'Diagnose why document view returns 404 (binding, ownership, file presence)';

    public function handle(): int
    {
        $docUuid = (string) $this->argument('doc_uuid');
        $caseUuid = (string) $this->argument('case_uuid');

        $case = ClientCase::where('uuid', $caseUuid)->first();
        if (! $case) {
            $this->error('ClientCase not found by uuid: '.$caseUuid);

            return self::FAILURE;
        }
        $this->info('ClientCase found: id='.$case->id.' uuid='.$case->uuid);

        $doc = Document::withTrashed()->where('uuid', $docUuid)->first();
        if (! $doc) {
            $this->error('Document not found by uuid (including trashed): '.$docUuid);

            return self::FAILURE;
        }
        $this->info('Document found: id='.$doc->id.' uuid='.$doc->uuid.' trashed='.(int) ($doc->deleted_at !== null));
        $this->line('  documentable_type='.$doc->documentable_type.' documentable_id='.$doc->documentable_id);
        $this->line('  disk='.$doc->disk.' path='.$doc->path);
        $this->line('  preview_path='.(string) $doc->preview_path.' preview_mime='.(string) $doc->preview_mime);

        // Ownership check like in controller
        $belongsToCase = $doc->documentable_type === ClientCase::class && $doc->documentable_id === $case->id;
        $belongsToContractOfCase = false;
        if ($doc->documentable_type === Contract::class) {
            $belongsToContractOfCase = Contract::withTrashed()
                ->where('id', $doc->documentable_id)
                ->where('client_case_id', $case->id)
                ->exists();
        }
        $this->line('Ownership: belongsToCase='.(int) $belongsToCase.' belongsToContractOfCase='.(int) $belongsToContractOfCase);

        // File existence checks
        $disk = $doc->disk ?: 'public';
        $relPath = ltrim($doc->path ?? '', '/\\');
        if (str_starts_with($relPath, 'public/')) {
            $relPath = substr($relPath, 7);
        }
        $existsOnDisk = Storage::disk($disk)->exists($relPath);
        $this->line('Source exists on disk='.$existsOnDisk.' (disk='.$disk.' relPath='.$relPath.')');
        if (! $existsOnDisk) {
            $publicFull = public_path($relPath);
            $this->line('Public candidate='.$publicFull.' exists='.(int) is_file($publicFull));
        }

        $previewDisk = config('files.preview_disk', 'public');
        $previewExists = $doc->preview_path ? Storage::disk($previewDisk)->exists($doc->preview_path) : false;
        $this->line('Preview exists on previewDisk='.$previewExists.' (disk='.$previewDisk.' path='.(string) $doc->preview_path.')');

        $this->info('Done. Compare with controller logic to pin the 404 branch.');

        return self::SUCCESS;
    }
}
