using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace FoodsharingSiegen.Server.Migrations
{
    /// <inheritdoc />
    public partial class ProposalModified : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<DateTime>(
                name: "Modified",
                table: "Prospects",
                type: "TEXT",
                nullable: true);
            
            // Fill Modified column of existing rows
            migrationBuilder.Sql(@"
                UPDATE Prospects
                SET Modified = COALESCE(
                    (SELECT MAX(Created) 
                     FROM Interactions 
                     WHERE Interactions.ProspectID = Prospects.Id),
                    Prospects.Created
                );
            ");
        }

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