using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace OTSSignsOrchestrator.Core.Migrations
{
    /// <inheritdoc />
    public partial class ReplaceCifsWithNfs : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // 1. Add new NFS columns
            migrationBuilder.AddColumn<string>(
                name: "NfsServer",
                table: "CmsInstances",
                type: "TEXT",
                maxLength: 200,
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "NfsExport",
                table: "CmsInstances",
                type: "TEXT",
                maxLength: 500,
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "NfsExportFolder",
                table: "CmsInstances",
                type: "TEXT",
                maxLength: 500,
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "NfsExtraOptions",
                table: "CmsInstances",
                type: "TEXT",
                maxLength: 500,
                nullable: true);

            // 2. Migrate existing CIFS data into NFS columns
            //    NfsServer = CifsServer, NfsExport = '/' + CifsShareName, NfsExportFolder = CifsShareFolder
            migrationBuilder.Sql(
                """
                UPDATE CmsInstances
                SET NfsServer       = CifsServer,
                    NfsExport       = CASE WHEN CifsShareName IS NOT NULL THEN '/' || CifsShareName ELSE NULL END,
                    NfsExportFolder = CifsShareFolder,
                    NfsExtraOptions = CifsExtraOptions
                WHERE CifsServer IS NOT NULL;
                """);

            // 3. Drop old CIFS columns
            migrationBuilder.DropColumn(name: "CifsServer",       table: "CmsInstances");
            migrationBuilder.DropColumn(name: "CifsShareName",    table: "CmsInstances");
            migrationBuilder.DropColumn(name: "CifsShareFolder",  table: "CmsInstances");
            migrationBuilder.DropColumn(name: "CifsUsername",      table: "CmsInstances");
            migrationBuilder.DropColumn(name: "CifsPassword",      table: "CmsInstances");
            migrationBuilder.DropColumn(name: "CifsExtraOptions", table: "CmsInstances");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            // Re-add CIFS columns
            migrationBuilder.AddColumn<string>(
                name: "CifsServer", table: "CmsInstances", type: "TEXT", maxLength: 200, nullable: true);
            migrationBuilder.AddColumn<string>(
                name: "CifsShareName", table: "CmsInstances", type: "TEXT", maxLength: 500, nullable: true);
            migrationBuilder.AddColumn<string>(
                name: "CifsShareFolder", table: "CmsInstances", type: "TEXT", maxLength: 500, nullable: true);
            migrationBuilder.AddColumn<string>(
                name: "CifsUsername", table: "CmsInstances", type: "TEXT", maxLength: 200, nullable: true);
            migrationBuilder.AddColumn<string>(
                name: "CifsPassword", table: "CmsInstances", type: "TEXT", maxLength: 1000, nullable: true);
            migrationBuilder.AddColumn<string>(
                name: "CifsExtraOptions", table: "CmsInstances", type: "TEXT", maxLength: 500, nullable: true);

            // Copy NFS data back to CIFS columns
            migrationBuilder.Sql(
                """
                UPDATE CmsInstances
                SET CifsServer       = NfsServer,
                    CifsShareName    = CASE WHEN NfsExport IS NOT NULL THEN LTRIM(NfsExport, '/') ELSE NULL END,
                    CifsShareFolder  = NfsExportFolder,
                    CifsExtraOptions = NfsExtraOptions
                WHERE NfsServer IS NOT NULL;
                """);

            // Drop NFS columns
            migrationBuilder.DropColumn(name: "NfsServer",       table: "CmsInstances");
            migrationBuilder.DropColumn(name: "NfsExport",       table: "CmsInstances");
            migrationBuilder.DropColumn(name: "NfsExportFolder", table: "CmsInstances");
            migrationBuilder.DropColumn(name: "NfsExtraOptions", table: "CmsInstances");
        }
    }
}
