<?php

namespace App\Filament\Student\Pages;

use Filament\Infolists\Components\ImageEntry;
use Filament\Infolists\Components\TextEntry;
use Filament\Pages\Page;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Concerns\InteractsWithSchemas;
use Filament\Schemas\Contracts\HasSchemas;
use Filament\Schemas\Schema;

class MyProfile extends Page implements HasSchemas
{
    use InteractsWithSchemas;

    protected string $view = 'filament.student.pages.my-profile';

    protected static ?string $navigationLabel = 'My Profile';

    protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-user';

    protected static ?int $navigationSort = 6;

    public function schema(Schema $schema): Schema
    {
        return $schema
            ->record(fn() => auth()->user()->studentProfile)
            ->components([
                Section::make('Personal Details')
                    ->columns(3)
                    ->schema([
                        TextEntry::make('dob')->label('Date of Birth')->date()
                            ->state(fn($record) => $record?->dob),
                        TextEntry::make('gender')
                            ->state(fn($record) => $record?->gender ? ucfirst($record->gender) : null),
                        TextEntry::make('phone')
                            ->state(fn($record) => $record?->phone),
                    ]),

                Section::make('Education Details')
                    ->columns(3)
                    ->schema([
                        TextEntry::make('college_name')
                            ->state(fn($record) => $record?->college_name),
                        TextEntry::make('degree')
                            ->state(fn($record) => $record?->degree),
                        TextEntry::make('graduation_year')
                            ->state(fn($record) => $record?->graduation_year),
                    ]),

                Section::make('Address')
                    ->columns(2)
                    ->schema([
                        TextEntry::make('address_line')
                            ->state(fn($record) => $record?->address_line),
                        TextEntry::make('city')
                            ->state(fn($record) => $record?->city),
                        TextEntry::make('state')
                            ->state(fn($record) => $record?->state),
                        TextEntry::make('zip')
                            ->state(fn($record) => $record?->zip),
                    ]),

                Section::make('Documents')
                    ->columns(4)
                    ->schema([
                        ImageEntry::make('photo_path')
                            ->label('Photo')
                            ->height(100)
                            ->state(fn($record) => $record?->photo_path),

                        TextEntry::make('id_proof_path')
                            ->label('ID Proof')
                            ->state(fn($record) => $record?->id_proof_path)
                            ->formatStateUsing(fn($state) => $state ? 'View' : 'Not Uploaded')
                            ->url(fn($state) => $state ? route('private.storage.serve', ['path' => $state]) : null)
                            ->openUrlInNewTab()
                            ->color(fn($state) => $state ? 'success' : 'gray')
                            ->icon('heroicon-o-document'),

                        TextEntry::make('education_proof_path')
                            ->label('Education Proof')
                            ->state(fn($record) => $record?->education_proof_path)
                            ->formatStateUsing(fn($state) => $state ? 'View' : 'Not Uploaded')
                            ->url(fn($state) => $state ? route('private.storage.serve', ['path' => $state]) : null)
                            ->openUrlInNewTab()
                            ->color(fn($state) => $state ? 'success' : 'gray')
                            ->icon('heroicon-o-academic-cap'),

                        TextEntry::make('recommendation_letter_path')
                            ->label('Recommendation Letter')
                            ->state(fn($record) => $record?->recommendation_letter_path)
                            ->formatStateUsing(fn($state) => $state ? 'View' : 'Not Uploaded')
                            ->url(fn($state) => $state ? route('private.storage.serve', ['path' => $state]) : null)
                            ->openUrlInNewTab()
                            ->color(fn($state) => $state ? 'success' : 'gray')
                            ->icon('heroicon-o-document-text'),
                    ]),
            ]);
    }
}
