<?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
    {
        // Schema::create('guard_patrol_entries', function (Blueprint $table) {
        //     $table->id();
        //     $table->timestamps();
        // });
        $sql = <<<'SQL'
            CREATE TABLE guard_patrol_entries (
                id                                  BIGINT GENERATED always AS IDENTITY PRIMARY KEY,

                plant_id                            BIGINT NOT NULL,
                guard_name_id                       BIGINT NOT NULL,
                check_point_name_id                 BIGINT NOT NULL,

                reader_code                         TEXT NULL,
                patrol_time                         TIMESTAMP NOT NULL DEFAULT NOW(),

                created_at                          TIMESTAMP NOT NULL DEFAULT NOW(),
                updated_at                          TIMESTAMP NOT NULL DEFAULT NOW(),
                deleted_at                          TIMESTAMP,

                created_by					        TEXT NOT NULL,
                updated_by					        TEXT NOT NULL,

                UNIQUE (plant_id, guard_name_id, check_point_name_id, patrol_time),
                FOREIGN KEY (plant_id)              REFERENCES  plants (id),
                FOREIGN KEY (guard_name_id)         REFERENCES  guard_names (id),
                FOREIGN KEY (check_point_name_id)   REFERENCES  check_point_names (id)
            );
        SQL;

        DB::statement($sql);
    }

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