using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace ColaFlow.Modules.Mcp.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class AddMcpApiKeys : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.EnsureSchema(
                name: "mcp");

            migrationBuilder.CreateTable(
                name: "mcp_api_keys",
                schema: "mcp",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
                    user_id = table.Column<Guid>(type: "uuid", nullable: false),
                    key_hash = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    key_prefix = table.Column<string>(type: "character varying(16)", maxLength: 16, nullable: false),
                    name = table.Column<string>(type: "character varying(255)", maxLength: 255, nullable: false),
                    description = table.Column<string>(type: "text", nullable: true),
                    permissions = table.Column<string>(type: "jsonb", nullable: false),
                    ip_whitelist = table.Column<string>(type: "jsonb", nullable: true),
                    status = table.Column<int>(type: "integer", nullable: false),
                    last_used_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    usage_count = table.Column<long>(type: "bigint", nullable: false),
                    created_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    expires_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    revoked_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    revoked_by = table.Column<Guid>(type: "uuid", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_mcp_api_keys", x => x.Id);
                });

            migrationBuilder.CreateIndex(
                name: "ix_mcp_api_keys_expires_at",
                schema: "mcp",
                table: "mcp_api_keys",
                column: "expires_at");

            migrationBuilder.CreateIndex(
                name: "ix_mcp_api_keys_key_prefix",
                schema: "mcp",
                table: "mcp_api_keys",
                column: "key_prefix",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "ix_mcp_api_keys_status",
                schema: "mcp",
                table: "mcp_api_keys",
                column: "status");

            migrationBuilder.CreateIndex(
                name: "ix_mcp_api_keys_tenant_id",
                schema: "mcp",
                table: "mcp_api_keys",
                column: "tenant_id");

            migrationBuilder.CreateIndex(
                name: "ix_mcp_api_keys_tenant_user",
                schema: "mcp",
                table: "mcp_api_keys",
                columns: new[] { "tenant_id", "user_id" });
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "mcp_api_keys",
                schema: "mcp");
        }
    }
}
