using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace DodoSSH.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class AddTeamInvitation : Migration
    {
        /// <inheritdoc />
        protected override void Up(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),
                    email = table.Column<string>(type: "citext", maxLength: 320, nullable: false),
                    role = table.Column<int>(type: "integer", nullable: false),
                    invited_by_user_id = table.Column<Guid>(type: "uuid", nullable: false),
                    created_at_utc = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    expires_at_utc = table.Column<DateTimeOffset>(type: "timestamp with time zone", 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),
                    revoked_at_utc = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    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");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "team_invitation",
                schema: "dodo");
        }
    }
}
