<?php

namespace App\Filament\Resources\Manage\Cooperations;

use App\Enums\RoleEnum;
use App\Filament\Resources\Manage\Cooperations\Infolists\CooperationInfolist;
use App\Filament\Resources\Manage\Cooperations\Pages\CreateCooperation;
use App\Filament\Resources\Manage\Cooperations\Pages\EditCooperation;
use App\Filament\Resources\Manage\Cooperations\Pages\ListCooperations;
use App\Filament\Resources\Manage\Cooperations\Pages\ViewCooperation;
use App\Filament\Resources\Manage\Cooperations\RelationManagers\CooperationMediaRelationManager;
use App\Filament\Resources\Manage\Cooperations\RelationManagers\CooperationPaymentRelationManager;
use App\Filament\Resources\Manage\Cooperations\RelationManagers\CooperationProposalRelationManager;
use App\Filament\Resources\Manage\Cooperations\RelationManagers\ReportRelationManager;
use App\Filament\Resources\Manage\Cooperations\RelationManagers\TaskAssignmentRelationManager;
use App\Filament\Resources\Manage\Cooperations\Schemas\CooperationForm;
use App\Filament\Resources\Manage\Cooperations\Tables\CooperationsTable;
use App\Models\Cooperation;
use BackedEnum;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use UnitEnum;

class CooperationResource extends Resource
{
    protected static ?string $model = Cooperation::class;

    protected static string|UnitEnum|null $navigationGroup = 'Kelola';

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

    protected static ?string $navigationLabel = 'Kerja Sama';

    protected static ?int $navigationSort = 7;

    protected static ?string $slug = 'manage/cooperations';

    public static function getNavigationBadge(): ?string
    {
        return static::getModel()::when(auth()->user()->hasRole(RoleEnum::PERUSAHAAN->value), function (Builder $query): void {
            $query->whereHas('partnerMedia.company', function (Builder $subQuery): void {
                $subQuery->where('user_id', auth()->id());
            });
        })
            ->count();
    }

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

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

    public static function infolist(Schema $schema): Schema
    {
        return CooperationInfolist::configure($schema);
    }

    public static function getRelations(): array
    {
        return [
            CooperationMediaRelationManager::class,
            CooperationProposalRelationManager::class,
            TaskAssignmentRelationManager::class,
            ReportRelationManager::class,
            CooperationPaymentRelationManager::class,
        ];
    }

    public static function getEloquentQuery(): Builder
    {
        return parent::getEloquentQuery()
            ->when(auth()->user()->hasRole(RoleEnum::PERUSAHAAN->value), function (Builder $query): void {
                $query->whereHas('cooperationMedia.partnerMedia.company', function (Builder $subQuery): void {
                    $subQuery->where('user_id', auth()->id());
                });
            });
    }

    public static function getPages(): array
    {
        return [
            'index' => ListCooperations::route('/'),
            'create' => CreateCooperation::route('/create'),
            'view' => ViewCooperation::route('/{record}'),
            'edit' => EditCooperation::route('/{record}/edit'),
        ];
    }
}
