using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace OTSSignsOrchestrator.Core.Migrations
{
    /// <inheritdoc />
    public partial class DesktopInitial : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "SecretMetadata",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "TEXT", nullable: false),
                    Name = table.Column<string>(type: "TEXT", maxLength: 200, nullable: false),
                    IsGlobal = table.Column<bool>(type: "INTEGER", nullable: false),
                    CustomerName = table.Column<string>(type: "TEXT", maxLength: 100, nullable: true),
                    CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false),
                    LastRotatedAt = table.Column<DateTime>(type: "TEXT", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_SecretMetadata", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "SshHosts",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "TEXT", nullable: false),
                    Label = table.Column<string>(type: "TEXT", maxLength: 200, nullable: false),
                    Host = table.Column<string>(type: "TEXT", maxLength: 500, nullable: false),
                    Port = table.Column<int>(type: "INTEGER", nullable: false),
                    Username = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
                    PrivateKeyPath = table.Column<string>(type: "TEXT", maxLength: 1000, nullable: true),
                    KeyPassphrase = table.Column<string>(type: "TEXT", maxLength: 2000, nullable: true),
                    Password = table.Column<string>(type: "TEXT", maxLength: 2000, nullable: true),
                    UseKeyAuth = table.Column<bool>(type: "INTEGER", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "TEXT", nullable: false),
                    LastTestedAt = table.Column<DateTime>(type: "TEXT", nullable: true),
                    LastTestSuccess = table.Column<bool>(type: "INTEGER", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_SshHosts", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "CmsInstances",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "TEXT", nullable: false),
                    CustomerName = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
                    StackName = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
                    CmsServerName = table.Column<string>(type: "TEXT", maxLength: 200, nullable: false),
                    HostHttpPort = table.Column<int>(type: "INTEGER", nullable: false),
                    ThemeHostPath = table.Column<string>(type: "TEXT", maxLength: 500, nullable: false),
                    LibraryHostPath = table.Column<string>(type: "TEXT", maxLength: 500, nullable: false),
                    SmtpServer = table.Column<string>(type: "TEXT", maxLength: 200, nullable: false),
                    SmtpUsername = table.Column<string>(type: "TEXT", maxLength: 200, nullable: false),
                    Constraints = table.Column<string>(type: "TEXT", maxLength: 2000, nullable: true),
                    TemplateRepoUrl = table.Column<string>(type: "TEXT", maxLength: 500, nullable: false),
                    TemplateRepoPat = table.Column<string>(type: "TEXT", maxLength: 500, nullable: true),
                    TemplateLastFetch = table.Column<DateTime>(type: "TEXT", nullable: true),
                    TemplateCacheKey = table.Column<string>(type: "TEXT", maxLength: 100, nullable: true),
                    Status = table.Column<int>(type: "INTEGER", nullable: false),
                    XiboUsername = table.Column<string>(type: "TEXT", maxLength: 500, nullable: true),
                    XiboPassword = table.Column<string>(type: "TEXT", maxLength: 1000, nullable: true),
                    XiboApiTestStatus = table.Column<int>(type: "INTEGER", nullable: false),
                    XiboApiTestedAt = table.Column<DateTime>(type: "TEXT", nullable: true),
                    CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "TEXT", nullable: false),
                    DeletedAt = table.Column<DateTime>(type: "TEXT", nullable: true),
                    SshHostId = table.Column<Guid>(type: "TEXT", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_CmsInstances", x => x.Id);
                    table.ForeignKey(
                        name: "FK_CmsInstances_SshHosts_SshHostId",
                        column: x => x.SshHostId,
                        principalTable: "SshHosts",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.SetNull);
                });

            migrationBuilder.CreateTable(
                name: "OperationLogs",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "TEXT", nullable: false),
                    Operation = table.Column<int>(type: "INTEGER", nullable: false),
                    InstanceId = table.Column<Guid>(type: "TEXT", nullable: true),
                    UserId = table.Column<string>(type: "TEXT", maxLength: 200, nullable: true),
                    Status = table.Column<int>(type: "INTEGER", nullable: false),
                    Message = table.Column<string>(type: "TEXT", maxLength: 2000, nullable: true),
                    DurationMs = table.Column<long>(type: "INTEGER", nullable: true),
                    Timestamp = table.Column<DateTime>(type: "TEXT", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_OperationLogs", x => x.Id);
                    table.ForeignKey(
                        name: "FK_OperationLogs_CmsInstances_InstanceId",
                        column: x => x.InstanceId,
                        principalTable: "CmsInstances",
                        principalColumn: "Id");
                });

            migrationBuilder.CreateIndex(
                name: "IX_CmsInstances_CustomerName",
                table: "CmsInstances",
                column: "CustomerName");

            migrationBuilder.CreateIndex(
                name: "IX_CmsInstances_SshHostId",
                table: "CmsInstances",
                column: "SshHostId");

            migrationBuilder.CreateIndex(
                name: "IX_CmsInstances_StackName",
                table: "CmsInstances",
                column: "StackName",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_OperationLogs_InstanceId",
                table: "OperationLogs",
                column: "InstanceId");

            migrationBuilder.CreateIndex(
                name: "IX_OperationLogs_Operation",
                table: "OperationLogs",
                column: "Operation");

            migrationBuilder.CreateIndex(
                name: "IX_OperationLogs_Timestamp",
                table: "OperationLogs",
                column: "Timestamp");

            migrationBuilder.CreateIndex(
                name: "IX_SecretMetadata_Name",
                table: "SecretMetadata",
                column: "Name",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_SshHosts_Label",
                table: "SshHosts",
                column: "Label",
                unique: true);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "OperationLogs");

            migrationBuilder.DropTable(
                name: "SecretMetadata");

            migrationBuilder.DropTable(
                name: "CmsInstances");

            migrationBuilder.DropTable(
                name: "SshHosts");
        }
    }
}
