<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

class AddDefaultScheduleForNewDoctor extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // Obtenemos el ID del último doctor creado
        $lastDoctor = DB::table('users')
            ->join('role_user', 'users.id', '=', 'role_user.user_id')
            ->where('role_user.role_id', 4) // ID del rol doctor
            ->orderBy('users.id', 'desc')
            ->first();

        if ($lastDoctor) {
            // Insertamos horarios de lunes a viernes (1-5)
            for ($day = 1; $day <= 5; $day++) {
                DB::table('doctor_schedules')->insert([
                    'doctor_id' => $lastDoctor->user_id,
                    'day_of_week' => $day,
                    'start_time' => '08:00:00',
                    'end_time' => '17:00:00',
                    'timezone' => 'America/Caracas',
                    'active' => true,
                    'created_at' => now(),
                    'updated_at' => now()
                ]);
            }
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // Obtenemos el ID del último doctor
        $lastDoctor = DB::table('users')
            ->join('role_user', 'users.id', '=', 'role_user.user_id')
            ->where('role_user.role_id', 4)
            ->orderBy('users.id', 'desc')
            ->first();

        if ($lastDoctor) {
            DB::table('doctor_schedules')
                ->where('doctor_id', $lastDoctor->user_id)
                ->delete();
        }
    }
} 