<?php

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

use App\Enums\NotifStyle;
use App\Filament\Support\CheerfulNotification;
use App\Models\Cooperation;
use Filament\Actions\Action;
use Filament\Infolists\Components\ImageEntry;
use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Joaopaulolndev\FilamentPdfViewer\Infolists\Components\PdfViewerEntry;

class CooperationInfolist
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make(fn () => CheerfulNotification::getByKey('cooperation.info_title'))
                    ->description(fn () => CheerfulNotification::getByKey('cooperation.info_desc'))
                    ->icon(fn () => CheerfulNotification::getNotifStyle() === NotifStyle::CHEERFUL ? 'heroicon-o-hand-raised' : null)
                    ->headerActions([
                        Action::make('status')
                            ->label(fn (Cooperation $record): ?string => $record->status->getLabel())
                            ->badge()
                            ->color(fn (Cooperation $record): string|array|null => $record->status->getColor())
                            ->disabled(),
                    ])
                    ->schema([
                        TextEntry::make('title')
                            ->label('Judul Kerja Sama'),

                        Grid::make(3)
                            ->schema([
                                TextEntry::make('initial_submission_date')
                                    ->label('Tanggal Pengajuan Awal')
                                    ->date('l, d F Y'),

                                TextEntry::make('final_submission_date')
                                    ->label('Tanggal Pengajuan Akhir')
                                    ->date('l, d F Y'),

                                TextEntry::make('taskAssignment.start_date')
                                    ->label('Tanggal Mulai Media Order')
                                    ->date('l, d F Y')
                                    ->placeholder('Belum ditentukan'),

                                TextEntry::make('taskAssignment.end_date')
                                    ->label('Tanggal Selesai Media Order')
                                    ->date('l, d F Y')
                                    ->placeholder('Belum ditentukan'),

                                TextEntry::make('payment_date')
                                    ->label('Tanggal Pembayaran')
                                    ->date('l, d F Y')
                                    ->placeholder('Belum dibayar'),

                                TextEntry::make('completed_at')
                                    ->label('Tanggal Selesai')
                                    ->date('l, d F Y')
                                    ->placeholder('Masih berjalan'),
                            ]),

                        Grid::make(3)
                            ->schema([
                                PdfViewerEntry::make('proposal_template')
                                    ->label('Template Proposal')
                                    ->getStateUsing(fn (Cooperation $record): ?string => optional($record->getMedia('cooperations')->where('custom_properties.doc_type', 'proposal-template')->sortByDesc('created_at')->first())->getPathRelativeToRoot())
                                    ->disk(config('filesystems.default'))
                                    ->columnSpan(2)
                                    ->visible(fn (Cooperation $record): bool => $record->getMedia('cooperations')->where('custom_properties.doc_type', 'proposal-template')->isNotEmpty()),

                                ImageEntry::make('banner')
                                    ->label('Banner')
                                    ->getStateUsing(fn (Cooperation $record): ?string => optional($record->getMedia('banner')->sortByDesc('created_at')->first() ?? $record->getMedia('cooperations')->where('custom_properties.doc_type', 'banner')->sortByDesc('created_at')->first())->getPathRelativeToRoot())
                                    ->disk(config('filesystems.default'))
                                    ->visible(fn (Cooperation $record): bool => $record->getMedia('banner')->isNotEmpty() || $record->getMedia('cooperations')->where('custom_properties.doc_type', 'banner')->isNotEmpty()),
                            ])
                            ->visible(fn (Cooperation $record): bool => $record->getMedia('banner')->isNotEmpty() || $record->getMedia('cooperations')->where('custom_properties.doc_type', 'banner')->isNotEmpty() || $record->getMedia('cooperations')->where('custom_properties.doc_type', 'proposal-template')->isNotEmpty()),

                        TextEntry::make('description')
                            ->label('Deskripsi')
                            ->html()
                            ->prose(),
                    ]),
            ])
            ->columns(1);
    }
}
