<?php

declare(strict_types=1);

namespace ImovelZapAi\Publishing\Presentation\Http\Controllers\Web;

use Illuminate\Http\Response;
use ImovelZapAi\Properties\Infrastructure\Persistence\Eloquent\PropertyModel;
use ImovelZapAi\Publishing\Application\Services\PropertyXmlExportStorage;

final class PropertyXmlExportController
{
    public function __construct(
        private readonly PropertyXmlExportStorage $storage,
    ) {}

    public function show(PropertyModel $property): Response
    {
        $xml = $this->storage->get($property);

        if ($xml === null) {
            abort(404, 'Exportação XML não encontrada para este imóvel.');
        }

        $filename = sprintf('%s.xml', $property->internal_code);

        return response($xml, 200, [
            'Content-Type' => 'application/xml; charset=UTF-8',
            'Content-Disposition' => 'attachment; filename="'.$filename.'"',
        ]);
    }
}
