using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace GerbilManagerWebAPI.Migrations
{
    /// <inheritdoc />
    public partial class AddSearchFields : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<string>(
                name: "NameSearch",
                table: "Gerbils",
                type: "text",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "OriginBreeder",
                table: "Gerbils",
                type: "text",
                nullable: true);

            // Backfill NameSearch for existing rows (lowercase, strip whitespace/.-_),
            // matching GerbilSearch.Normalize. New/updated rows stay in sync via
            // ApplicationContext.SaveChanges.
            migrationBuilder.Sql(
                "UPDATE \"Gerbils\" SET \"NameSearch\" = lower(regexp_replace(\"Name\", '[[:space:]._-]', '', 'g'));");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "NameSearch",
                table: "Gerbils");

            migrationBuilder.DropColumn(
                name: "OriginBreeder",
                table: "Gerbils");
        }
    }
}
