<?php

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

use App\Enums\IssueSentiment;
use App\Enums\NotifStyle;
use App\Filament\Support\CheerfulNotification;
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(fn () => CheerfulNotification::getByKey('issue.identification_title'))
                    ->description(fn () => CheerfulNotification::getByKey('issue.identification_desc'))
                    ->icon(fn () => CheerfulNotification::getNotifStyle() === NotifStyle::CHEERFUL ? 'heroicon-o-magnifying-glass' : null)
                    ->schema([
                        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.'),

                        Select::make('departments')
                            ->label('Perangkat Daerah Terkait')
                            ->relationship('departments', 'name', fn (Builder $query): Builder => $query->active())
                            ->searchable()
                            ->preload()
                            ->multiple()
                            ->required(),

                        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('Sublokus')
                                    ->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 Isu')
                            ->placeholder('Jelaskan detail isu yang berkembang...')
                            ->required()
                            ->columnSpanFull(),
                    ])->columnSpan(2),

                Section::make(fn () => CheerfulNotification::getByKey('issue.sentiment_title'))
                    ->description(fn () => CheerfulNotification::getByKey('issue.sentiment_desc'))
                    ->icon(fn () => CheerfulNotification::getNotifStyle() === NotifStyle::CHEERFUL ? 'heroicon-o-chart-bar' : null)
                    ->schema([
                        Select::make('issue')
                            ->label('Sentimen Isu')
                            ->options(IssueSentiment::options())
                            ->required()
                            ->native(false)
                            ->in(implode(',', array_column(IssueSentiment::cases(), 'value'))),

                        Select::make('response')
                            ->label('Respon Pemerintah')
                            ->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('Subklasifikasi')
                            ->relationship('subClassification', 'name', fn (Builder $query, Get $get): Builder => $query->where('classification_id', $get('classification_id'))->active())
                            ->searchable()
                            ->preload()
                            ->required(),
                    ])->columnSpan(1),
            ])
            ->columns(3);
    }
}
