<?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
    {
        if (Schema::hasTable('pre_invoices')) {
            Schema::rename('pre_invoices', 'invoices');
        } else {
            Schema::create('invoices', function (Blueprint $table) {
                $table->increments('id');
                $table->integer('user_id')->unsigned(); // the client

                // the client data are archived in this table to pevent any loss of data among the invoices
                $table->string('client_name')->nullable(); // client name.
                $table->string('client_idnumber')->nullable(); // client passport.
                $table->string('client_address')->nullable(); // client address.
                $table->string('client_email')->nullable(); // client email.
                $table->string('client_phone')->nullable(); // client email.

                $table->integer('company_id')->default(1); // link to copany details: name, legal number... - todo
                $table->string('invoice_number')->nullable(); // the final invoice number, generated by the accounting software
                $table->timestamps();
                $table->softDeletes();
            });
        }

        if (Schema::hasTable('pre_invoice_details')) {
            Schema::rename('pre_invoice_details', 'invoice_details');
        } else {
            Schema::create('invoice_details', function (Blueprint $table) {
                $table->increments('id');
                $table->integer('invoice_id')->unsigned(); // pre_invoice id
                $table->string('product_name');
                $table->bigInteger('price');
                $table->string('product_code')->nullable();
                $table->integer('product_id')->nullable();
                $table->string('product_type')->nullable();
                $table->timestamps();
                $table->softDeletes();
            });
        }

        if (Schema::hasColumn('invoice_details', 'pre_invoice_id')) {
            Schema::table('invoice_details', function (Blueprint $table) {
                if (DB::connection()->getPDO()->getAttribute(PDO::ATTR_DRIVER_NAME) !== 'sqlite') {
                    $table->dropForeign('pre_invoice_details_pre_invoice_id_foreign');
                }

                $table->renameColumn('pre_invoice_id', 'invoice_id');
            });
        }

        if (Schema::hasColumn('payments', 'pre_invoice_id')) {
            Schema::table('payments', function (Blueprint $table) {
                if (DB::connection()->getPDO()->getAttribute(PDO::ATTR_DRIVER_NAME) !== 'sqlite') {
                    $table->dropForeign('payments_pre_invoice_id_foreign');
                }

                $table->renameColumn('pre_invoice_id', 'invoice_id');
            });
        }
    }

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