<?php

declare(strict_types=1);

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
    {
        // 1. Create Couriers Table
        Schema::create('couriers', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('phone')->nullable();
            $table->string('vehicle_number')->nullable();
            $table->boolean('is_active')->default(true);
            $table->foreignId('outlet_id')->nullable()->constrained('outlets')->onDelete('set null');
            $table->timestamps();
        });

        // 2. Create Vouchers Table
        Schema::create('vouchers', function (Blueprint $table) {
            $table->id();
            $table->string('code')->unique();
            $table->string('type'); // 'percent', 'nominal'
            $table->decimal('value', 10, 2);
            $table->decimal('min_purchase', 10, 2)->default(0);
            $table->decimal('max_discount', 10, 2)->nullable();
            $table->integer('usage_limit')->nullable();
            $table->integer('usage_count')->default(0);
            $table->date('expires_at')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        // 3. Add birth_date to Customers Table
        Schema::table('customers', function (Blueprint $table) {
            $table->date('birth_date')->nullable();
        });

        // 4. Add courier_id and delivery_status to Transactions Table
        Schema::table('transactions', function (Blueprint $table) {
            $table->foreignId('courier_id')->nullable()->constrained('couriers')->onDelete('set null');
            $table->string('delivery_status')->nullable(); // 'pending', 'out_for_delivery', 'delivered', 'failed'
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('transactions', function (Blueprint $table) {
            $table->dropForeign(['courier_id']);
            $table->dropColumn(['courier_id', 'delivery_status']);
        });

        Schema::table('customers', function (Blueprint $table) {
            $table->dropColumn('birth_date');
        });

        Schema::dropIfExists('vouchers');
        Schema::dropIfExists('couriers');
    }
};
