<?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()
    {
		Schema::create('account__provider', function (Blueprint $table)
		{
			$table->id();
			$table->timestamps();
			$table->integer('site_id')->unsigned();
			$table->string('ref');
			$table->integer('synctoken');

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

			$table->integer('provider_oauth_id')->unsigned();
			$table->foreign(['provider_oauth_id','site_id'])->references(['id','site_id'])->on('provider_oauth');
		});

		Schema::create('invoice__provider', function (Blueprint $table)
		{
			$table->timestamps();
			$table->integer('invoice_id')->unsigned();
			$table->integer('provider_oauth_id')->unsigned();
			$table->integer('site_id')->unsigned();
			$table->string('ref');
			$table->integer('synctoken');

			$table->foreign(['invoice_id','site_id'])->references(['id','site_id'])->on('invoices');
			$table->foreign(['provider_oauth_id','site_id'])->references(['id','site_id'])->on('provider_oauth');
		});

		Schema::create('product__provider', function (Blueprint $table)
		{
			$table->integer('product_id')->unsigned();
			$table->integer('provider_oauth_id')->unsigned();
			$table->integer('site_id')->unsigned();
			$table->string('ref');

			$table->foreign(['product_id','site_id'])->references(['id','site_id'])->on('products');
			$table->foreign(['provider_oauth_id','site_id'])->references(['id','site_id'])->on('provider_oauth');
		});

		Schema::create('tax__provider', function (Blueprint $table)
		{
			$table->timestamps();
			$table->integer('tax_id')->unsigned();
			$table->integer('provider_oauth_id')->unsigned();
			$table->integer('site_id')->unsigned();
			$table->string('ref');
			$table->integer('synctoken');

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

		Schema::create('payment__provider', function (Blueprint $table)
		{
			$table->timestamps();
			$table->integer('payment_id')->unsigned();
			$table->integer('provider_oauth_id')->unsigned();
			$table->integer('site_id')->unsigned();
			$table->string('ref');
			$table->integer('synctoken');

			$table->foreign(['payment_id','site_id'])->references(['id','site_id'])->on('payments');
			$table->foreign(['provider_oauth_id','site_id'])->references(['id','site_id'])->on('provider_oauth');
		});
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
		Schema::dropIfExists('payment__provider');
		Schema::dropIfExists('tax__provider');
		Schema::dropIfExists('product__provider');
		Schema::dropIfExists('invoice__provider');
        Schema::dropIfExists('account__provider');
    }
};