<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     * 
     * Changes hours_per_day to hours_per_week as the key metric.
     * Default: 40 hours/week (8h × 5 days)
     * Derives: hours_per_day = hours_per_week / working_days_per_week
     */
    public function up(): void
    {
        Schema::table('business_settings', function (Blueprint $table) {
            // Add hours_per_week column
            $table->decimal('hours_per_week', 5, 2)->default(40.00)->after('employees_count');
            // Add working_days_per_week (for calculation)
            $table->integer('working_days_per_week')->default(5)->after('hours_per_week');
        });

        // Convert existing hours_per_day to hours_per_week
        // hours_per_week = hours_per_day * 5 (assuming 5-day week)
        DB::table('business_settings')->update([
            'hours_per_week' => DB::raw('hours_per_day * 5'),
            'working_days_per_week' => 5,
        ]);

        // Remove old hours_per_day column
        Schema::table('business_settings', function (Blueprint $table) {
            $table->dropColumn('hours_per_day');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('business_settings', function (Blueprint $table) {
            $table->decimal('hours_per_day', 4, 2)->default(8.00)->after('employees_count');
        });

        // Convert back: hours_per_day = hours_per_week / 5
        DB::table('business_settings')->update([
            'hours_per_day' => DB::raw('hours_per_week / 5'),
        ]);

        Schema::table('business_settings', function (Blueprint $table) {
            $table->dropColumn(['hours_per_week', 'working_days_per_week']);
        });
    }
};
