<?php

namespace App\Filament\Resources;

use App\Filament\Resources\TeacherResource\Pages;
use App\Models\Teacher;
use App\Models\User;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class TeacherResource extends Resource
{
    protected static ?string $model = Teacher::class;

    protected static bool $shouldRegisterNavigation = false;

    public static function form(Schema $schema): Schema
    {
        return $schema->schema([
            TextInput::make('teacher_id')
                ->label('教师ID')
                ->disabled()
                ->hidden(fn (?Teacher $record) => blank($record))
                ->formatStateUsing(fn (?Teacher $record): string => $record?->teacher_id ?? ''),
            TextInput::make('name')
                ->label('教师姓名')
                ->required()
                ->maxLength(128)
                ->placeholder('请输入教师姓名'),
            TextInput::make('subject')
                ->label('教授科目')
                ->required()
                ->maxLength(64)
                ->placeholder('例如：数学、语文、英语等'),
            TextInput::make('user.username')
                ->label('手机号（登录用户名）')
                ->required()
                ->regex('/^1[3-9]\d{9}$/')
                ->placeholder('请输入11位手机号（将作为登录用户名）')
                ->helperText('请输入11位手机号码，将作为登录用户名')
                ->maxLength(11)
                ->minLength(11),
            TextInput::make('user.email')
                ->label('邮箱地址（可选）')
                ->email()
                ->nullable()
                ->placeholder('请输入邮箱地址（可选）'),
            TextInput::make('user.password_hash')
                ->label('密码')
                ->required()
                ->password()
                ->revealable()
                ->dehydrateStateUsing(fn ($state) => Hash::make($state))
                ->placeholder('请输入密码'),
            Textarea::make('remark')
                ->label('备注')
                ->rows(3)
                ->placeholder('请输入备注信息（可选）')
                ->columnSpanFull(),
        ])->columns(2);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('teacher_id')
                    ->label('教师ID')
                    ->badge()
                    ->color('primary')
                    ->copyable()
                    ->copyMessage('教师ID已复制')
                    ->copyMessageDuration(1500)
                    ->sortable()
                    ->searchable(),
                Tables\Columns\TextColumn::make('name')
                    ->label('姓名')
                    ->weight('bold')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('subject')
                    ->label('教授科目')
                    ->badge()
                    ->color('info')
                    ->sortable(),
                Tables\Columns\TextColumn::make('user.username')
                    ->label('手机号（登录名）')
                    ->badge()
                    ->color('primary')
                    ->copyable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('user.phone')
                    ->label('备用手机号')
                    ->copyable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('user.email')
                    ->label('邮箱')
                    ->copyable()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('students_count')
                    ->label('学生数量')
                    ->counts('students')
                    ->sortable()
                    ->alignCenter()
                    ->badge()
                    ->color('success'),
                Tables\Columns\TextColumn::make('created_at')
                    ->label('创建时间')
                    ->dateTime('Y-m-d H:i')
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\SelectFilter::make('subject')
                    ->label('教授科目')
                    ->options(fn () => self::subjectOptions())
                    ->placeholder('全部科目'),
            ])
            ->actions([])
            ->bulkActions([])
            ->emptyStateHeading('暂无教师记录')
            ->emptyStateDescription('开始添加第一位教师吧')
            ->emptyStateActions([]);
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListTeachers::route('/'),
            'create' => Pages\CreateTeacher::route('/create'),
            'view' => Pages\ViewTeacher::route('/{record}'),
            'edit' => Pages\EditTeacher::route('/{record}/edit'),
        ];
    }

    protected static function subjectOptions(): array
    {
        return [
            'math' => '数学',
            'chinese' => '语文',
            'english' => '英语',
            'physics' => '物理',
            'chemistry' => '化学',
            'biology' => '生物',
            'history' => '历史',
            'geography' => '地理',
            'politics' => '政治',
            'other' => '其他',
        ];
    }
}