<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // Get all restaurants
        $restaurants = DB::table('restaurants')->get();
        
        // Set default timezone (+05:00 for Pakistan) for all existing restaurants
        foreach ($restaurants as $restaurant) {
            // Check if timezone setting already exists
            $existing = PosSetting::where('restaurant_id', $restaurant->id)
                ->whereNull('branch_id')
                ->where('key', 'timezone')
                ->first();
            
            if (!$existing) {
                PosSetting::create([
                    'restaurant_id' => $restaurant->id,
                    'branch_id' => null,
                    'key' => 'timezone',
                    'value' => '+05:00',
                    'type' => 'string',
                    'description' => 'Restaurant timezone (e.g., +05:00 for Pakistan, Asia/Karachi)',
                    'created_at' => now(),
                    'updated_at' => now(),
                ]);
            }
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // Remove timezone settings
        PosSetting::where('key', 'timezone')->delete();
    }
};
