<?php

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

class CreateCashCollectionHistoryTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('cash_collection_history', function (Blueprint $table) {
            $table->id();
            $table->string('delivery_agent_id');
            $table->unsignedBigInteger('order_id')->nullable();
            $table->foreign('order_id')
              ->references('id')
              ->on('orders')
              ->onDelete('cascade');
            $table->decimal('amount', 10, 2);
            $table->dateTime('transaction_date');
            $table->timestamps();

            // Set up foreign key relationship for delivery_agent_id
            $table->foreign('delivery_agent_id')
                  ->references('user_id')
                  ->on('drivers')
                  ->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('cash_collection_history', function (Blueprint $table) {
            $table->dropForeign(['delivery_agent_id']);
            $table->dropForeign(['order_id']);
        });

        Schema::dropIfExists('cash_collection_history');
    }
}
