<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('m_periods', function (Blueprint $table) {
            $table->id();
            $table->string('code', 20)->unique();
            $table->string('name', 100);
            $table->text('description')->nullable();
            $table->boolean('is_active')->default(true);
            $table->boolean('delete_flag')->default(false);
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->timestamps();

            $table->index(['delete_flag', 'is_active']);
            $table->index('code');
        });

        // Insert default periods
        DB::table('m_periods')->insert([
            [
                'code' => 'HARIAN',
                'name' => 'Harian',
                'description' => 'Periode order harian',
                'is_active' => true,
                'delete_flag' => false,
                'created_at' => now(),
                'updated_at' => now()
            ],
            [
                'code' => 'PERIODIK',
                'name' => 'Periodik',
                'description' => 'Periode order periodik',
                'is_active' => true,
                'delete_flag' => false,
                'created_at' => now(),
                'updated_at' => now()
            ]
        ]);
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('m_periods');
    }
};