<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('git_repositories', function (Blueprint $table) {
            $table->id();
            $table->foreignId('account_id')->constrained('accounts')->cascadeOnDelete();
            $table->string('name', 100);
            $table->string('remote_url', 500)->nullable();
            $table->string('local_path', 500);
            $table->string('default_branch', 100)->default('main');
            $table->enum('deploy_strategy', ['none', 'auto_pull', 'webhook_pull', 'symlink'])->default('none');
            $table->string('deploy_target_path', 500)->nullable();
            $table->text('deploy_script')->nullable();
            $table->string('webhook_secret', 64)->nullable();
            $table->boolean('is_private')->default(false);
            $table->text('ssh_deploy_key_private')->nullable();
            $table->text('ssh_deploy_key_public')->nullable();
            $table->timestamp('last_pull_at')->nullable();
            $table->string('last_commit_sha', 40)->nullable();
            $table->string('last_commit_message', 500)->nullable();
            $table->string('last_commit_author', 191)->nullable();
            $table->enum('status', ['cloning', 'ready', 'error', 'archived'])->default('cloning');
            $table->text('last_error')->nullable();
            $table->timestamps();
            $table->softDeletes();
            $table->unique(['account_id', 'name']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('git_repositories');
    }
};
