using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace DodoSSH.Infrastructure.Migrations
{
    /// <summary>
    /// Removes the invitation table. Membership is now only ever granted to an account that exists.
    /// </summary>
    /// <remarks>
    /// <para>
    /// <b>Pending invitations are dropped rather than converted, and that is the decision rather than
    /// the omission.</b> Converting one would mean creating a membership because an address matched —
    /// which is the exact property this change exists to remove, and the same one
    /// <c>OidcOptions.AllowEmailLinking</c> refuses one door along. In practice almost nothing is lost:
    /// an invitation to an address that already had an account here was claimed within the hour by the
    /// sign-in sweep, so what is left in this table is offers to people who never arrived, and there
    /// was never an account to make a membership out of.
    /// </para>
    /// <para>
    /// The Down is a faithful rebuild of the empty table and nothing else. It cannot bring the rows
    /// back, and a migration that pretended otherwise would be worse than one that says so.
    /// </para>
    /// </remarks>
    public partial class DropTeamInvitation : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "team_invitation",
                schema: "dodo");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "team_invitation",
                schema: "dodo",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false),
                    team_id = table.Column<Guid>(type: "uuid", nullable: false),
                    accepted_at_utc = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    accepted_by_user_id = table.Column<Guid>(type: "uuid", nullable: true),
                    created_at_utc = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    email = table.Column<string>(type: "citext", maxLength: 320, nullable: false),
                    expires_at_utc = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    invited_by_user_id = table.Column<Guid>(type: "uuid", nullable: false),
                    revoked_at_utc = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    role = table.Column<int>(type: "integer", nullable: false),
                    xmin = table.Column<uint>(type: "xid", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_team_invitation", x => x.id);
                    table.ForeignKey(
                        name: "fk_team_invitation_team_team_id",
                        column: x => x.team_id,
                        principalSchema: "dodo",
                        principalTable: "team",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "ix_team_invitation_email",
                schema: "dodo",
                table: "team_invitation",
                column: "email");

            migrationBuilder.CreateIndex(
                name: "ix_team_invitation_team_id_email",
                schema: "dodo",
                table: "team_invitation",
                columns: new[] { "team_id", "email" },
                unique: true,
                filter: "accepted_at_utc IS NULL AND revoked_at_utc IS NULL");
        }
    }
}
