<?php

namespace Lucent\Commands;

use Illuminate\Console\Command;
use Illuminate\Database\Schema\Blueprint;
use Lucent\Database\Database;

class SetupDatabase extends Command
{

    protected $signature = 'lucent:setup-db';

    protected $description = 'Run to setup a new database';


    public function handle()
    {

        $dbConnection = config("lucent.database");

        $databasePath = config("database.connections.$dbConnection.database");
        if(file_exists($databasePath)){
            $this->error("Database already exists.");
            return 0;
        }
        touch($databasePath);
        $this->tableUsers();
        $this->tableRecords();
        $this->tableRevisions();
        $this->tableSessions();
        $this->tableCommandLogs();

        $this->info("Lucent Database Setup Completed");
    }

    private function tableUsers(): void
    {
        Database::make()->getSchemaBuilder()->create('users', function (Blueprint $table) {
            $table->uuid("id")->primary();
            $table->string('name')->nullable();
            $table->string('email')->unique();
            $table->jsonb('roles');
            $table->string('createdAt');
            $table->string('updatedAt');
            $table->string('loggedInAt');
            $table->string('mailToken')->nullable();

        });
    }

    private function tableSessions(): void
    {
        Database::make()->getSchemaBuilder()->create('sessions', function (Blueprint $table) {
            $table->string('id')->primary();
            $table->foreignId('user_id')->nullable()->index();
            $table->string('ip_address', 45)->nullable();
            $table->text('user_agent')->nullable();
            $table->longText('payload');
            $table->integer('last_activity')->index();
        });
    }

    private function tableRecords(): void
    {
        Database::make()->getSchemaBuilder()->create('records', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->string('schema');
            $table->string('status');
            $table->jsonb('data');
            $table->jsonb('_sys');
            $table->jsonb('_file');
            $table->text('search')->default("");

            $table->index(['schema', '_sys->updatedAt', 'status']);
            $table->index('search');
        });

        Database::make()->getSchemaBuilder()->create('edges', function (Blueprint $table) {
            $table->uuid('source');
            $table->uuid('target');
            $table->string('field');
            $table->string('rank');
            $table->unique(['source', 'target', "field"]);
        });
    }

    private function tableRevisions(): void
    {
        Database::make()->getSchemaBuilder()->create('revisions', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->uuid('recordId');
            $table->string('schema');
            $table->jsonb('data');
            $table->jsonb('_sys');
            $table->jsonb('_file');
            $table->jsonb('_edges');
        });
    }

    private function tableCommandLogs(): void
    {
        Database::make()->getSchemaBuilder()->create('command_logs', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->string('signature');
            $table->integer('pid')->nullable();
            $table->text('logs');
        });
    }

}
