<?php

namespace Modules\Inventory\DTO;

use Carbon\Carbon;
use Illuminate\Http\Request;
use Modules\Billing\Enums\BillPayStatusEnum;
use Modules\Inventory\Enums\PurchaseStatusEnum;

class PurchaseInvoiceListFilter
{
    public function __construct(
        public readonly ?string $search = null,
        public readonly ?int $supplierId = null,
        public readonly ?PurchaseStatusEnum $status = null,
        public readonly ?BillPayStatusEnum $paymentStatus = null,
        public readonly ?string $fromDate = null,
        public readonly ?string $toDate = null,
        public readonly ?int $perPage = 15,
    ) {}

    public static function fromRequest(Request $request): self
    {
        return new self(
            search: $request->filled('search') ? trim($request->search) : null,
            supplierId: $request->filled('supplier_id') ? (int) $request->query('supplier_id') : null,
            status: $request->filled('status') ? PurchaseStatusEnum::tryFrom($request->query('status')) : null,
            paymentStatus: $request->filled('payment_status') ? BillPayStatusEnum::tryFrom($request->query('payment_status')) : null,
            fromDate: $request->filled('from_date') ? Carbon::parse($request->query('from_date')) : null,
            toDate: $request->filled('to_date') ? Carbon::parse($request->query('to_date')) : null,
            perPage: (int) $request->query('per_page', 15)
        );
    }
}
