<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('barbers', function (Blueprint $table) {
            $table->json('weekly_schedule')->nullable()->after('specialty');
        });

        $rows = DB::table('barbers')->select('id', 'working_days', 'work_start', 'work_end')->get();
        foreach ($rows as $row) {
            $days = json_decode($row->working_days ?? '[]', true);
            if (! is_array($days)) {
                $days = [];
            }
            $start = Carbon::parse($row->work_start ?? '09:00')->format('H:i');
            $end = Carbon::parse($row->work_end ?? '17:00')->format('H:i');
            $schedule = [];
            for ($w = 0; $w <= 6; $w++) {
                $schedule[] = [
                    'weekday' => $w,
                    'enabled' => in_array($w, $days, true),
                    'start' => $start,
                    'end' => $end,
                ];
            }
            DB::table('barbers')->where('id', $row->id)->update([
                'weekly_schedule' => json_encode($schedule),
            ]);
        }

        Schema::table('barbers', function (Blueprint $table) {
            $table->dropColumn(['working_days', 'work_start', 'work_end']);
        });
    }

    public function down(): void
    {
        Schema::table('barbers', function (Blueprint $table) {
            $table->json('working_days')->nullable()->after('specialty');
            $table->time('work_start')->default('09:00:00')->after('working_days');
            $table->time('work_end')->default('17:00:00')->after('work_start');
        });

        $rows = DB::table('barbers')->select('id', 'weekly_schedule')->get();
        foreach ($rows as $row) {
            $schedule = json_decode($row->weekly_schedule ?? '[]', true);
            if (! is_array($schedule)) {
                $schedule = [];
            }
            $enabledDays = [];
            $firstStart = '09:00';
            $firstEnd = '17:00';
            foreach ($schedule as $item) {
                $w = (int) ($item['weekday'] ?? -1);
                if (! empty($item['enabled']) && $w >= 0 && $w <= 6) {
                    $enabledDays[] = $w;
                    $firstStart = substr((string) ($item['start'] ?? '09:00'), 0, 5);
                    $firstEnd = substr((string) ($item['end'] ?? '17:00'), 0, 5);
                }
            }
            DB::table('barbers')->where('id', $row->id)->update([
                'working_days' => json_encode($enabledDays ?: [1, 2, 3, 4, 5]),
                'work_start' => $firstStart,
                'work_end' => $firstEnd,
            ]);
        }

        Schema::table('barbers', function (Blueprint $table) {
            $table->dropColumn('weekly_schedule');
        });
    }
};
