using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace VigilCareClinicalAPI.Migrations
{
    /// <inheritdoc />
    public partial class AddPatientNameSearchToken : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AlterColumn<string>(
                name: "date_of_birth",
                table: "patients",
                type: "text",
                nullable: false,
                oldClrType: typeof(DateOnly),
                oldType: "date");

            migrationBuilder.AddColumn<string>(
                name: "name_search_token",
                table: "patients",
                type: "character varying(64)",
                maxLength: 64,
                nullable: true);

            migrationBuilder.CreateIndex(
                name: "IX_patients_name_search_token",
                table: "patients",
                column: "name_search_token");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropIndex(
                name: "IX_patients_name_search_token",
                table: "patients");

            migrationBuilder.DropColumn(
                name: "name_search_token",
                table: "patients");

            migrationBuilder.Sql(
                """
                UPDATE patients SET date_of_birth = '1900-01-01' WHERE date_of_birth !~ '^\d{4}-\d{2}-\d{2}$';
                ALTER TABLE patients ALTER COLUMN date_of_birth TYPE date USING date_of_birth::date;
                """);
        }
    }
}
