<?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('t_forecasts', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('store_id');
            $table->date('upload_date');
            $table->integer('year');
            $table->integer('month');
            $table->string('forecast_number', 50)->unique();
            $table->enum('status', ['Draft', 'Confirmed', 'Cancelled'])->default('Draft');
            $table->text('notes')->nullable();
            $table->boolean('delete_flag')->default(0);
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->timestamps();

            // Foreign keys
            $table->foreign('store_id')->references('id')->on('m_stores');
            $table->foreign('created_by')->references('id')->on('users');
            $table->foreign('updated_by')->references('id')->on('users');

            // Indexes
            $table->index(['store_id', 'year', 'month']);
            $table->index(['status']);
            $table->index(['delete_flag']);
        });
    }

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