<?php

namespace Database\Seeders;

use App\Models\Roster;
use App\Models\RosterResultRow;
use App\Models\RosterResultUpload;
use App\Models\TaskSubmission;
use App\Models\TaskSubmissionItem;
use App\Models\User;
use App\Support\RosterData;
use Carbon\CarbonImmutable;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        if (Schema::hasTable('personal_access_tokens')) {
            DB::table('personal_access_tokens')->truncate();
        }

        if (Schema::hasTable('task_submission_items')) {
            TaskSubmissionItem::query()->delete();
        }

        if (Schema::hasTable('task_submissions')) {
            TaskSubmission::query()->delete();
        }

        RosterResultRow::query()->delete();
        RosterResultUpload::query()->delete();
        Roster::query()->delete();
        User::query()->delete();

        $admin = User::query()->create([
            'name' => 'Admin',
            'email' => 'admin@cs.com',
            'password' => 'admin123',
            'role' => 'admin',
            'team' => 'CS Team',
        ]);

        $auditor = User::query()->create([
            'name' => 'Auditor',
            'email' => 'auditor@cs.com',
            'password' => 'admin123',
            'role' => 'auditor',
            'team' => 'CS Team',
        ]);

        $user = User::query()->create([
            'name' => 'Test User',
            'email' => 'user@cs.com',
            'password' => 'admin123',
            'role' => 'user',
            'team' => 'CS Team',
        ]);

        $rosterDate = CarbonImmutable::today()->toDateString();

        $rosterRows = [
            [
                'user' => $admin,
                'team' => 'CS Team',
                'start' => '09:00',
                'end' => '17:00',
                'hours' => 8,
            ],
            [
                'user' => $auditor,
                'team' => 'CS Team',
                'start' => '10:00',
                'end' => '18:00',
                'hours' => 8,
            ],
            [
                'user' => $user,
                'team' => 'CS Team',
                'start' => '08:30',
                'end' => '16:30',
                'hours' => 8,
            ],
        ];

        foreach ($rosterRows as $row) {
            Roster::query()->create([
                'user_id' => $row['user']->id,
                'team' => $row['team'],
                'roster_date' => $rosterDate,
                'shift1_start' => "{$rosterDate} {$row['start']}:00",
                'shift1_end' => "{$rosterDate} {$row['end']}:00",
                'shift2_start' => null,
                'shift2_end' => null,
                'shift3_start' => null,
                'shift3_end' => null,
                'total_hours' => $row['hours'],
                'is_holiday' => false,
            ]);
        }

        $designUpload = RosterResultUpload::query()->create([
            'team' => 'CS Team',
            'roster_date' => $rosterDate,
            'uploaded_by_user_id' => $admin->id,
            'source_filename' => "csteam-roster-{$rosterDate}.csv",
            'imported_rows' => 2,
            'matched_at' => now(),
        ]);

        RosterResultRow::query()->create([
            'upload_id' => $designUpload->id,
            'team' => 'CS Team',
            'roster_date' => $rosterDate,
            'user_name' => $admin->name,
            'normalized_user_name' => RosterData::normalizeName($admin->name),
            'total_hours' => 8,
            'raw_payload' => ['user' => $admin->name, 'total_hours' => 8],
        ]);

        RosterResultRow::query()->create([
            'upload_id' => $designUpload->id,
            'team' => 'CS Team',
            'roster_date' => $rosterDate,
            'user_name' => $user->name,
            'normalized_user_name' => RosterData::normalizeName($user->name),
            'total_hours' => 8,
            'raw_payload' => ['user' => $user->name, 'total_hours' => 8],
        ]);

        RosterResultRow::query()->create([
            'upload_id' => $designUpload->id,
            'team' => 'CS Team',
            'roster_date' => $rosterDate,
            'user_name' => $auditor->name,
            'normalized_user_name' => RosterData::normalizeName($auditor->name),
            'total_hours' => 8,
            'raw_payload' => ['user' => $auditor->name, 'total_hours' => 8],
        ]);
    }
}
