<?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('insurance_policies', function (Blueprint $table) {
            $table->id();
            // Foreign key referencing an existing Entity record.
            $table->unsignedBigInteger('entity_id');
            $table->foreign('entity_id')
                ->references('id')->on('entities')
                ->onDelete('cascade');

            $table->unsignedBigInteger('insurer_id');
            $table->foreign('insurer_id')
                ->references('id')->on('insurers')
                ->onDelete('cascade');

            $table->unsignedBigInteger('commission_default_id');
            $table->foreign('commission_default_id')
                ->references('id')->on('commission_defaults')
                ->onDelete('cascade');

            $table->string('policy_number')->unique();
            $table->string('custom_name')->nullable();
            $table->decimal('commission_pct_default', 10, 2);
            $table->decimal('commission_pct', 10, 2)->nullable();
            $table->decimal('expected_commission', 10, 2)->nullable();
            $table->decimal('insured_amount', 10, 2)->nullable();
            $table->decimal('premium', 10, 2)->nullable(); // premium = cost of the insurance policy
            $table->date('start_date')->nullable();
            $table->date('end_date')->nullable();
            $table->string('status')->default('active');
            $table->timestamps();
        });
    }

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