<?php

namespace Webkul\Purchase\Filament\Admin\Clusters\Orders\Resources;

use BackedEnum;
use Filament\Resources\Pages\Page;
use Filament\Support\Icons\Heroicon;
use Webkul\Purchase\Enums\RequisitionType;
use Webkul\Purchase\Filament\Admin\Clusters\Orders;
use Webkul\Purchase\Filament\Admin\Clusters\Orders\Resources\QuotationResource\Pages\CreateQuotation;
use Webkul\Purchase\Filament\Admin\Clusters\Orders\Resources\QuotationResource\Pages\EditQuotation;
use Webkul\Purchase\Filament\Admin\Clusters\Orders\Resources\QuotationResource\Pages\ListQuotations;
use Webkul\Purchase\Filament\Admin\Clusters\Orders\Resources\QuotationResource\Pages\ManageBills;
use Webkul\Purchase\Filament\Admin\Clusters\Orders\Resources\QuotationResource\Pages\ManageReceipts;
use Webkul\Purchase\Filament\Admin\Clusters\Orders\Resources\QuotationResource\Pages\ViewQuotation;
use Webkul\Purchase\Models\Quotation;
use Webkul\Purchase\Models\RequisitionLine;

class QuotationResource extends OrderResource
{
    protected static ?string $model = Quotation::class;

    protected static string|BackedEnum|null $navigationIcon = Heroicon::DocumentText;

    protected static bool $shouldRegisterNavigation = true;

    protected static bool $isGloballySearchable = true;

    protected static ?string $recordTitleAttribute = 'name';

    protected static ?int $navigationSort = 1;

    protected static ?string $cluster = Orders::class;

    public static function getNavigationLabel(): string
    {
        return __('purchases::filament/admin/clusters/orders/resources/quotation.navigation.title');
    }

    public static function getModelLabel(): string
    {
        return __('purchases::filament/admin/clusters/orders/resources/quotation.navigation.title');
    }

    public static function getRecordSubNavigation(Page $page): array
    {
        return $page->generateNavigationItems([
            ViewQuotation::class,
            EditQuotation::class,
            ManageBills::class,
            ManageReceipts::class,
        ]);
    }

    public static function getPages(): array
    {
        return [
            'index'    => ListQuotations::route('/'),
            'create'   => CreateQuotation::route('/create'),
            'view'     => ViewQuotation::route('/{record}'),
            'edit'     => EditQuotation::route('/{record}/edit'),
            'bills'    => ManageBills::route('/{record}/bills'),
            'operations' => ManageReceipts::route('/{record}/receipts'),
        ];
    }

    protected static function getAgreementDefaultQuantity(RequisitionLine $line): float|int
    {
        $type = $line->requisition?->type;

        if ($type == RequisitionType::BLANKET_ORDER->value) {
            return (float) 0;
        }

        return (float) ($line->ordered_qty ?? 0);
    }
}
