<?php

namespace App\Filament\Resources\ResellerOrders;

use App\Filament\Resources\ResellerOrders\Pages\CreateResellerOrder;
use App\Filament\Resources\ResellerOrders\Pages\EditResellerOrder;
use App\Filament\Resources\ResellerOrders\Pages\ListResellerOrders;
use App\Filament\Resources\ResellerOrders\Schemas\ResellerOrderForm;
use App\Filament\Resources\ResellerOrders\Tables\ResellerOrdersTable;
use App\Models\Order;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Tables\Table;

class ResellerOrderResource extends Resource
{
    protected static ?string $model = Order::class;

    protected static string|\UnitEnum|null $navigationGroup = 'Reseller System';

    protected static ?int $navigationSort = 1;

    protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-shopping-bag';

    protected static ?string $navigationLabel = 'Reseller Orders';

    protected static ?string $recordTitleAttribute = 'order_number';

    public static function getNavigationBadge(): ?string
    {
        return static::getModel()::where('is_reseller_order', true)->count() ?: null;
    }

    public static function getNavigationBadgeColor(): ?string
    {
        return 'gray';
    }

    public static function getGloballySearchableAttributes(): array
    {
        return ['order_number', 'customer_name', 'customer_phone'];
    }

    public static function getEloquentQuery(): \Illuminate\Database\Eloquent\Builder
    {
        return parent::getEloquentQuery()->with(['childOrders'])->where('is_reseller_order', true);
    }

    public static function form(Schema $schema): Schema
    {
        return ResellerOrderForm::configure($schema);
    }

    public static function table(Table $table): Table
    {
        return ResellerOrdersTable::configure($table);
    }

    public static function getRelations(): array
    {
        return [
            RelationManagers\TransactionsRelationManager::class,
            RelationManagers\OrderHistoriesRelationManager::class,
        ];
    }

    public static function getWidgets(): array
    {
        return [
            Widgets\ResellerOrderStats::class,
        ];
    }

    public static function getPages(): array
    {
        return [
            'index'  => ListResellerOrders::route('/'),
            'create' => CreateResellerOrder::route('/create'),
            'edit'   => EditResellerOrder::route('/{record}/edit'),
        ];
    }
}
