<?php

namespace App\Filament\Vendor\Resources;

use App\Models\QuotationItem;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;

class QuotationItemResource extends Resource
{
    protected static ?string $model = QuotationItem::class;
    protected static ?string $navigationIcon = 'heroicon-o-clipboard-document-list';
    protected static ?string $navigationLabel = 'My Quotation Requests';

    /** Vendors only see items for products they own. Unit price is intentionally excluded. */
    public static function getEloquentQuery(): Builder
    {
        return parent::getEloquentQuery()
            ->where('vendor_id', auth()->id())
            ->with(['quotation.customer', 'product']);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('quotation.reference_number')->label('Quotation Ref'),
                Tables\Columns\TextColumn::make('quotation.customer.name')->label('Customer'),
                Tables\Columns\TextColumn::make('product.name')->label('Product')->default('Custom Item'),
                Tables\Columns\TextColumn::make('quantity'),
                Tables\Columns\BadgeColumn::make('quotation.status')->label('Quote Status'),
                // unit_price is intentionally NOT shown to vendors
            ])
            ->recordUrl(null);
    }

    public static function canCreate(): bool { return false; }
    public static function canEdit($record): bool { return false; }
    public static function canDelete($record): bool { return false; }

    public static function getPages(): array
    {
        return [
            'index' => \App\Filament\Vendor\Resources\QuotationItemResource\Pages\ListQuotationItems::route('/'),
        ];
    }
}
