<?php

namespace App\Filament\Resources\Monitoring\IssueManagements\Schemas;

use App\Enums\IssueSentiment;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\Select;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Components\Utilities\Set;
use Filament\Schemas\Schema;
use Illuminate\Database\Eloquent\Builder;

class IssueManagementForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make([
                    Select::make('media_monitoring_id')
                        ->label('Monitoring Media')
                        ->relationship(
                            'mediaMonitoring',
                            'title',
                            fn (Builder $query): Builder => $query->whereDoesntHave('issueManagement')
                        )
                        ->searchable()
                        ->preload()
                        ->required()
                        ->autofocus()
                        ->helperText('Jika judul berita dari monitoring media tidak ada di opsi, silakan lakukan pencarian.'),

                    Grid::make(2)
                        ->schema([
                            Select::make('location_id')
                                ->label('Lokus')
                                ->relationship('location', 'name', fn (Builder $query): Builder => $query->active())
                                ->searchable()
                                ->preload()
                                ->live()
                                ->afterStateUpdated(function (Set $set): void {
                                    $set('sub_location_id', null);
                                })
                                ->required(),

                            Select::make('sub_location_id')
                                ->label('Sub Lokus')
                                ->relationship('subLocation', 'name', fn (Builder $query, Get $get): Builder => $query->active()->where('location_id', $get('location_id')))
                                ->searchable()
                                ->preload()
                                ->required(),
                        ]),

                    RichEditor::make('description')
                        ->label('Keterangan')
                        ->placeholder('...')
                        ->required()
                        ->columnSpanFull(),
                ])->columnSpan(2),

                Section::make([
                    Select::make('issue')
                        ->label('Isu')
                        ->options(IssueSentiment::options())
                        ->required()
                        ->native(false)
                        ->in(implode(',', array_column(IssueSentiment::cases(), 'value'))),

                    Select::make('response')
                        ->label('Respon')
                        ->options(IssueSentiment::options())
                        ->required()
                        ->native(false)
                        ->in(implode(',', array_column(IssueSentiment::cases(), 'value'))),

                    Select::make('classification_id')
                        ->label('Klasifikasi')
                        ->relationship('classification', 'name', fn (Builder $query): Builder => $query->active())
                        ->searchable()
                        ->preload()
                        ->live()
                        ->afterStateUpdated(function (Set $set): void {
                            $set('sub_classification_id', null);
                        })
                        ->required(),

                    Select::make('sub_classification_id')
                        ->label('Sub Klasifikasi')
                        ->relationship('subClassification', 'name', fn (Builder $query, Get $get): Builder => $query->where('classification_id', $get('classification_id'))->active())
                        ->searchable()
                        ->preload()
                        ->required(),
                ])->columnSpan(1),
            ])
            ->columns(3);
    }
}
