<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class SprintSeeder extends Seeder
{
    public function run(): void
    {
        $projects = DB::table('projects')->pluck('id');
        $now = now();

        DB::table('sprints')->insert([
            [
                'project_id' => $projects[0],
                'title' => 'اسپرینت ۱ - فروردین',
                'start_date' => '2026-01-05',
                'end_date' => '2026-02-05',
                'status' => 'active',
                'goal' => 'تکمیل ماژول حضور و غیاب و مدیریت مرخصی با قابلیت ثبت و گزارش‌گیری',
                'created_by' => 1,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'project_id' => $projects[0],
                'title' => 'اسپرینت ۲ - اردیبهشت',
                'start_date' => '2026-02-06',
                'end_date' => '2026-03-06',
                'status' => 'planning',
                'goal' => 'تکمیل محاسبات حقوق و دستمزد و طراحی فرم‌های ارزیابی',
                'created_by' => 1,
                'created_at' => $now,
                'updated_at' => $now,
            ],
        ]);

        $sprints = DB::table('sprints')->pluck('id');
        $tasks = DB::table('tasks')->where('project_id', $projects[0])->pluck('id');

        $sprintTasks = [];
        $taskIndex = 0;
        foreach ($tasks as $taskId) {
            $sprintIndex = $taskIndex < 4 ? 0 : 1;
            $sprintTasks[] = [
                'sprint_id' => $sprints[$sprintIndex],
                'task_id' => $taskId,
                'created_at' => $now,
                'updated_at' => $now,
            ];
            $taskIndex++;
        }

        DB::table('sprint_task')->insert($sprintTasks);
    }
}
