<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use phpDocumentor\Reflection\Types\Nullable;

class CreateAccountsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('accounts', function (Blueprint $table) {
            $table->id();
            $table->string('uid', 30)->unique();
            $table->bigInteger('owner_id')->unsigned();
            $table->string('name', 100);
            $table->json('settings');
            $table->bigInteger('plan_id')->unsigned();
            $table->bigInteger('billing_address_id')->unsigned()->nullable();
            $table->bigInteger('space_usage_images')->default(0);
            $table->bigInteger('space_usage_zips')->default(0);
            $table->bigInteger('space_usage_archive')->default(0);
            $table->string('logo', 255)->nullable();
            $table->string('slug', 255)->nullable()->unique();
            $table->string('custom_domain', 255)->nullable()->unique();
            $table->string('stripe_id')->nullable()->index();
            $table->string('card_brand')->nullable();
            $table->string('card_last_four', 4)->nullable();
            $table->timestamp('trial_ends_at')->nullable();
            $table->timestamps();

            $table->foreign('owner_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');

            $table->foreign('plan_id')
                ->references('id')
                ->on('plans')
                ->onDelete('restrict');

            $table->foreign('billing_address_id')
                ->references('id')
                ->on('addresses')
                ->onDelete('set null');
        });
    }

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