<?php

namespace App\Http\Controllers\Api\Quotes;

use App\Domains\Quotes\Models\Quote;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;

class QuoteIndexController extends Controller
{
    public function __invoke(): JsonResponse
    {
        $quotes = Quote::query()
            ->with(['contact', 'creator'])
            ->where('tenant_id', request()->user()->tenant_id)
            ->orderByDesc('created_at')
            ->get()
            ->map(fn (Quote $quote) => [
                'id' => $quote->id,
                'quoteNumber' => $quote->quote_number,
                'contact' => $quote->contact?->primary_name,
                'status' => $quote->status,
                'total' => '$'.number_format((float) $quote->total, 2),
                'expiry' => $quote->expires_at?->format('M d, Y'),
                'creator' => $quote->creator?->full_name ?? 'Unknown',
            ])
            ->values();

        return response()->json([
            'data' => $quotes,
        ]);
    }
}
