<?php

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

return new class extends Migration {
    public function up(): void
    {
        // Hapus kolom tambahan di tabel users bawaan Laravel
        // (role, phone, foto, is_active) jika belum ada
        if (Schema::hasTable('users')) {
            Schema::table('users', function (Blueprint $table) {
                if (!Schema::hasColumn('users', 'phone')) {
                    $table->string('phone')->nullable()->after('email');
                }
                if (!Schema::hasColumn('users', 'foto')) {
                    $table->string('foto')->nullable()->after('phone');
                }
                if (!Schema::hasColumn('users', 'role')) {
                    $table->enum('role', ['customer', 'admin'])->default('customer')->after('foto');
                }
                if (!Schema::hasColumn('users', 'is_active')) {
                    $table->boolean('is_active')->default(true)->after('role');
                }
            });
        }

        // Buat tabel lapangan
        Schema::create('lapangan', function (Blueprint $table) {
            $table->id();
            $table->string('nama');
            $table->text('deskripsi');
            $table->enum('jenis_olahraga', [
                'futsal', 'badminton', 'basket',
                'voli', 'tenis', 'sepakbola', 'lainnya'
            ]);
            $table->integer('harga_per_jam');
            $table->string('lokasi');
            $table->text('alamat')->nullable();
            $table->json('foto')->nullable();
            $table->json('fasilitas')->nullable();
            $table->time('jam_buka')->default('08:00:00');
            $table->time('jam_tutup')->default('22:00:00');
            $table->boolean('is_active')->default(true);
            $table->decimal('rating', 3, 1)->nullable();
            $table->integer('total_review')->default(0);
            $table->unsignedBigInteger('owner_id');
            $table->foreign('owner_id')->references('id')->on('users')->onDelete('cascade');
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('lapangan');
    }
};
