<?php

namespace Tests\Tenant;

use Illuminate\Support\Facades\Storage;
use Mtc\Crm\Models\FormQuestion;
use Mtc\MercuryDataModels\Enquiry;
use Mtc\MercuryDataModels\Form;
use Tests\TenantTestCase;

class MigrateEnquiryFilesToTenantPathTest extends TenantTestCase
{
    public function testLegacyUploadsAreMovedToTenantPrefixedPath()
    {
        Storage::fake('file-storage');

        $legacy = 'uploads/2026-Jun/document.pdf';
        Storage::disk('file-storage')->put($legacy, 'pdf-contents');

        $form = Form::factory()->create();
        $question = FormQuestion::factory()->create([
            'form_id' => $form->id,
            'type' => 'file_upload',
            'name' => 'Document',
        ]);

        $enquiry = Enquiry::factory()->create([
            'form_id' => $form->id,
            'details' => [
                ['id' => $question->id, 'question' => 'Document', 'answer' => [$legacy]],
            ],
        ]);
        $enquiry->answers()->create([
            'question_id' => $question->id,
            'question_name' => 'Document',
            'answer_array' => [$legacy],
            'answer_type' => 'file',
        ]);

        $this->artisan('enquiries:migrate-files-to-tenant-path')->assertExitCode(0);

        $expected = tenant('id') . '/' . $legacy;
        Storage::disk('file-storage')->assertExists($expected);
        Storage::disk('file-storage')->assertMissing($legacy);

        $enquiry->refresh();
        $this->assertEquals([$expected], $enquiry->details[0]['answer']);
        $this->assertEquals([$expected], $enquiry->answers()->first()->answer_array);
    }

    public function testAlreadyPrefixedUploadsAreLeftUntouched()
    {
        Storage::fake('file-storage');

        $alreadyMigrated = tenant('id') . '/uploads/2026-Jun/document.pdf';
        Storage::disk('file-storage')->put($alreadyMigrated, 'pdf-contents');

        $form = Form::factory()->create();
        $question = FormQuestion::factory()->create([
            'form_id' => $form->id,
            'type' => 'file_upload',
            'name' => 'Document',
        ]);
        $enquiry = Enquiry::factory()->create([
            'form_id' => $form->id,
            'details' => [
                ['id' => $question->id, 'question' => 'Document', 'answer' => $alreadyMigrated],
            ],
        ]);

        $this->artisan('enquiries:migrate-files-to-tenant-path')->assertExitCode(0);

        Storage::disk('file-storage')->assertExists($alreadyMigrated);
        $enquiry->refresh();
        $this->assertEquals($alreadyMigrated, $enquiry->details[0]['answer']);
    }

    public function testDryRunMovesNothingAndLeavesReferences()
    {
        Storage::fake('file-storage');

        $legacy = 'uploads/2026-Jun/document.pdf';
        Storage::disk('file-storage')->put($legacy, 'pdf-contents');

        $form = Form::factory()->create();
        $question = FormQuestion::factory()->create([
            'form_id' => $form->id,
            'type' => 'file_upload',
            'name' => 'Document',
        ]);
        $enquiry = Enquiry::factory()->create([
            'form_id' => $form->id,
            'details' => [
                ['id' => $question->id, 'question' => 'Document', 'answer' => $legacy],
            ],
        ]);

        $this->artisan('enquiries:migrate-files-to-tenant-path', ['--dry-run' => true])->assertExitCode(0);

        Storage::disk('file-storage')->assertExists($legacy);
        Storage::disk('file-storage')->assertMissing(tenant('id') . '/' . $legacy);

        $enquiry->refresh();
        $this->assertEquals($legacy, $enquiry->details[0]['answer']);
    }
}
