<?php

namespace App\Filament\Resources\Audits\Schemas;

use App\Models\Activity;
use App\Models\CashRecord;
use App\Models\User;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Schemas\Schema;

class AuditForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema->components([
            Select::make('auditor_id')->label('Auditor')
                ->options(User::pluck('name', 'id'))
                ->searchable()
                ->required(),
            Select::make('model_type')->label('Tipe Objek')
                ->options([
                    'App\\Models\\CashRecord' => 'Transaksi Kas',
                    'App\\Models\\Activity'   => 'Kegiatan',
                    'App\\Models\\User'       => 'Anggota',
                ])
                ->required()
                ->live(),
            Select::make('model_id')->label('Objek')
                ->options(fn ($get) => match ($get('model_type')) {
                    'App\\Models\\CashRecord' => CashRecord::get()
                        ->mapWithKeys(fn ($r) => [$r->id => "{$r->description} — Rp " . number_format($r->amount, 0, ',', '.')]),
                    'App\\Models\\Activity'   => Activity::pluck('title', 'id'),
                    'App\\Models\\User'       => User::pluck('name', 'id'),
                    default                   => [],
                })
                ->searchable()
                ->required(),
            Select::make('issue_type')->label('Jenis Temuan')
                ->options(['warning' => '⚠️ Peringatan', 'critical' => '🚨 Kritis'])
                ->required(),
            Textarea::make('description')->label('Deskripsi Temuan')->rows(3)->required()->columnSpanFull(),
            Select::make('status')
                ->options(['open' => 'Terbuka', 'resolved' => 'Selesai'])
                ->default('open')
                ->required(),
        ]);
    }
}
