<?php

namespace App\Filament\Resources\Companies;

use App\Filament\Resources\Companies\Pages\CreateCompany;
use App\Filament\Resources\Companies\Pages\EditCompany;
use App\Filament\Resources\Companies\Pages\ListCompanies;
use App\Models\Company;
use BackedEnum;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Table;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Section;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\IconColumn;
use Filament\Actions\EditAction;
use Filament\Actions\DeleteAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;

class CompanyResource extends Resource
{
    protected static ?string $model = Company::class;

    protected static ?string $navigationLabel = null;

    protected static ?string $modelLabel = null;

    protected static ?string $pluralModelLabel = null;

    protected static ?int $navigationSort = 1;
    protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedBuildingOffice2;

    public static function getNavigationGroup(): string|null
    {
        return 'البيانات الأساسية';
    }

    public static function getNavigationBadge(): ?string
    {
        return static::getModel()::count();
    }

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make(__('filament/admin/company_resource.المعلوماتالأساسية'))
                    ->schema([
                        TextInput::make('code')
                            ->label(__('filament/admin/company_resource.code'))
                            ->disabled()
                            ->dehydrated(false)
                            ->placeholder('يولد تلقائياً'),

                        TextInput::make('name')
                            ->label(__('filament/admin/company_resource.name'))
                            ->required()
                            ->maxLength(255),

                        TextInput::make('tax_no')
                            ->label(__('filament/admin/company_resource.tax_no'))
                            ->maxLength(50),

                        TextInput::make('contact_person')
                            ->label(__('filament/admin/company_resource.contact_person'))
                            ->maxLength(255),
                    ])->columns(2),

                Section::make(__('filament/admin/company_resource.معلوماتالاتصال'))
                    ->schema([
                        TextInput::make('phone')
                            ->label(__('filament/admin/company_resource.phone'))
                            ->tel()
                            ->maxLength(20),

                        TextInput::make('email')
                            ->label(__('filament/admin/company_resource.email'))
                            ->email()
                            ->maxLength(255),

                        Textarea::make('address')
                            ->label(__('filament/admin/company_resource.address'))
                            ->maxLength(500)
                            ->columnSpanFull(),
                    ])->columns(2),

                Section::make(__('filament/admin/company_resource.تفاصيلإضافية'))
                    ->schema([
                        Textarea::make('notes')
                            ->label(__('filament/admin/company_resource.notes'))
                            ->maxLength(1000)
                            ->columnSpanFull(),

                        Toggle::make('is_active')
                            ->label(__('filament/admin/company_resource.is_active'))
                            ->default(true),
                    ]),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->applyTenantScope()
            ->columns([
                TextColumn::make('code')
                    ->label(__('filament/admin/company_resource.code'))
                    ->searchable()
                    ->sortable(),
                
                TextColumn::make('name')
                    ->label(__('filament/admin/company_resource.name'))
                    ->searchable()
                    ->sortable(),
                
                TextColumn::make('tax_no')
                    ->label(__('filament/admin/company_resource.tax_no'))
                    ->searchable()
                    ->toggleable(),
                
                TextColumn::make('phone')
                    ->label(__('filament/admin/company_resource.phone'))
                    ->searchable()
                    ->toggleable(),
                
                TextColumn::make('contact_person')
                    ->label(__('filament/admin/company_resource.contact_person'))
                    ->searchable()
                    ->toggleable(),
                
                IconColumn::make('is_active')
                    ->label(__('filament/admin/company_resource.is_active'))
                    ->boolean(),
            ])
            ->recordActions([
                EditAction::make(),
                DeleteAction::make()->requiresConfirmation(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make()->requiresConfirmation(),
                ]),
            ]);
    }

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

    public static function canViewAny(): bool
    {
        return true;
    }

    public static function canCreate(): bool
    {
        return true;
    }

    public static function canEdit($record): bool
    {
        return true;
    }

    public static function canDelete($record): bool
    {
        return true;
    }

    public static function canDeleteAny(): bool
    {
        return true;
    }
    public static function getNavigationLabel(): string
    {
        return __('filament/admin/company_resource.navigation_label');
    }
    public static function getModelLabel(): string
    {
        return __('filament/admin/company_resource.model_label');
    }
    public static function getPluralModelLabel(): string
    {
        return __('filament/admin/company_resource.plural_model_label');
    }



}
