<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
		DB::statement('ALTER TABLE ab_invoice RENAME TO invoices');
		DB::statement('ALTER TABLE invoices MODIFY account_id int unsigned NOT NULL');
		DB::statement('ALTER TABLE invoices MODIFY account_billing_id int unsigned DEFAULT NULL');
		DB::statement('ALTER TABLE invoices MODIFY active tinyint(1) NOT NULL,MODIFY process_status tinyint(1) DEFAULT NULL,MODIFY billing_status tinyint(1) DEFAULT NULL,MODIFY print_status tinyint(1) DEFAULT NULL');

		Schema::table('invoices', function (Blueprint $table) {
			$table->dropPrimary();
			$table->dropForeign('ab_invoice_site_id_foreign');
			$table->dropIndex('ab_invoice_site_id_foreign');
			$table->primary(['id','site_id']);
			$table->dropIndex('ab_invoice_id_site_id_index');
			$table->datetime('created_at')->nullable()->after('id');
			$table->datetime('updated_at')->nullable()->after('created_at');
			$table->date('due_at')->nullable()->after('discount_amt');
		});

		// Convert out dates
		foreach (\App\Models\Invoice::withoutGlobalScope(\App\Models\Scopes\SiteScope::class)->cursor() as $o) {
			if ($o->date_orig)
				$o->created_at = \Carbon\Carbon::createFromTimestamp($o->date_orig);
			if ($o->date_last)
				$o->updated_at = \Carbon\Carbon::createFromTimestamp($o->date_last);
			if ($o->getRawOriginal('due_date'))
				$o->due_at = \Carbon\Carbon::createFromTimestamp($o->getRawOriginal('due_date'));
			if ($o->reminders) {
				try {
					$reminders = unserialize($o->reminders);
				} catch (Exception $e) {
					$reminders = unserialize(gzuncompress($o->reminders));
				}

				$o->reminders = $reminders;
			}
			$o->save();
		}

		Schema::table('invoices', function (Blueprint $table) {
			$table->dropColumn(['date_orig','date_last','due_date']);

			$table->foreign(['account_id','site_id'])->references(['id','site_id'])->on('accounts');
		});

		DB::statement('ALTER TABLE ab_invoice_item RENAME TO invoice_items');
		DB::statement('ALTER TABLE invoice_items MODIFY invoice_id int unsigned NOT NULL');
		DB::statement('ALTER TABLE invoice_items MODIFY service_id int unsigned DEFAULT NULL');
		DB::statement('ALTER TABLE invoice_items MODIFY product_id int unsigned DEFAULT NULL');
		DB::statement('ALTER TABLE invoice_items MODIFY module_id int unsigned DEFAULT NULL');
		DB::statement('ALTER TABLE invoice_items MODIFY module_ref int unsigned DEFAULT NULL');
		DB::statement('ALTER TABLE invoice_items RENAME COLUMN recurring_schedule TO recur_schedule');

		Schema::table('invoice_items', function (Blueprint $table) {
			$table->dropPrimary();
			$table->dropForeign('ab_invoice_item_site_id_foreign');
			$table->dropIndex('ab_invoice_item_site_id_foreign');
			$table->primary(['id','site_id']);
			$table->dropIndex('ab_invoice_item_id_site_id_index');
			$table->datetime('created_at')->nullable()->after('id');
			$table->datetime('updated_at')->nullable()->after('created_at');
			$table->date('start_at')->nullable()->after('recur_schedule');
			$table->date('stop_at')->nullable()->after('start_at');
		});

		// Convert out dates
		foreach (\App\Models\InvoiceItem::withoutGlobalScope(\App\Models\Scopes\SiteScope::class)->cursor() as $o) {
			if ($o->date_orig)
				$o->created_at = \Carbon\Carbon::createFromTimestamp($o->date_orig);
			if ($o->date_last)
				$o->updated_at = \Carbon\Carbon::createFromTimestamp($o->date_last);
			if ($o->date_start)
				$o->start_at = \Carbon\Carbon::createFromTimestamp($o->date_start);
			if ($o->date_stop)
				$o->stop_at = \Carbon\Carbon::createFromTimestamp($o->date_stop);
			$o->save();
		}

		Schema::table('invoice_items', function (Blueprint $table) {
			$table->dropColumn(['date_orig','date_last','date_start','date_stop']);

			$table->foreign(['service_id','site_id'])->references(['id','site_id'])->on('services');
			$table->foreign(['invoice_id','site_id'])->references(['id','site_id'])->on('invoices');
			$table->foreign(['product_id','site_id'])->references(['id','site_id'])->on('products');
		});

		DB::statement('ALTER TABLE ab_invoice_item_tax RENAME TO invoice_item_taxes');
		DB::statement('ALTER TABLE invoice_item_taxes MODIFY invoice_item_id int unsigned NOT NULL');
		DB::statement('ALTER TABLE invoice_item_taxes MODIFY tax_id int unsigned NOT NULL');
		DB::statement('ALTER TABLE invoice_item_taxes DROP PRIMARY KEY,ADD PRIMARY KEY (id,site_id)');

		Schema::table('invoice_item_taxes', function (Blueprint $table) {
			$table->dropForeign('ab_invoice_item_tax_site_id_foreign');
			$table->dropIndex('ab_invoice_item_tax_site_id_foreign');
			$table->dropIndex('ab_invoice_item_tax_id_site_id_index');
			$table->dropColumn(['date_orig']);

			$table->foreign(['invoice_item_id','site_id'])->references(['id','site_id'])->on('invoice_items');
			$table->foreign(['tax_id'])->references(['id'])->on('taxes');
		});
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        abort(500,'Cant go back');
    }
};