<?php

namespace App\Filament\Resources\Manage\Cooperations\RelationManagers;

use App\Enums\ApprovalStatus;
use App\Models\TaskAssignment;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\CreateAction;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class TaskAssignmentRelationManager extends RelationManager
{
    protected static string $relationship = 'taskAssignments';

    protected static ?string $title = 'Penugasan';

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                DatePicker::make('start_date')
                    ->label('Tanggal Mulai')
                    ->required()
                    ->native(false),

                DatePicker::make('end_date')
                    ->label('Tanggal Selesai')
                    ->required()
                    ->native(false)
                    ->after('start_date'),

                Textarea::make('task_description')
                    ->label('Deskripsi Tugas')
                    ->required()
                    ->rows(4)
                    ->maxLength(65535),

                TextInput::make('report_amount')
                    ->label('Jumlah Laporan')
                    ->numeric()
                    ->required()
                    ->minValue(1)
                    ->default(1),

                Select::make('partner_media_ids')
                    ->label('Media yang Ditugaskan')
                    ->relationship('partnerMedia', 'name')
                    ->multiple()
                    ->preload()
                    ->required(),

                Select::make('status')
                    ->options(ApprovalStatus::options())
                    ->required()
                    ->default(ApprovalStatus::PENDING),

                Textarea::make('rejection_reason')
                    ->label('Alasan Penolakan')
                    ->rows(3)
                    ->columnSpanFull(),
            ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('task_description')
            ->columns([
                TextColumn::make('start_date')
                    ->label('Mulai')
                    ->date('l, d F Y'),

                TextColumn::make('end_date')
                    ->label('Selesai')
                    ->date('l, d F Y'),

                TextColumn::make('task_description')
                    ->label('Deskripsi Tugas')
                    ->limit(50),

                TextColumn::make('report_amount')
                    ->label('Jumlah Laporan'),
                TextColumn::make('report_count')
                    ->label('Laporan Diterima')
                    ->getStateUsing(function (TaskAssignment $record) {
                        return $record->reports()->count().' / '.($record->report_amount * $record->partnerMedia()->count());
                    }),
            ])
            ->filters([
                //
            ])
            ->headerActions([
                CreateAction::make()
                    ->label('Buat Penugasan')
                    ->visible(fn () => $this->canCreateTaskAssignment()),
            ])
            ->recordActions([
                EditAction::make(),

                DeleteAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }

    protected function canCreateTaskAssignment(): bool
    {
        $cooperation = $this->getOwnerRecord();

        // Can create task assignment only if there are accepted proposals
        return $cooperation->proposals()->where('status', ApprovalStatus::ACCEPTED)->exists();
    }
}
