using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace FiscalOS.Infra.Migrations
{
  /// <inheritdoc />
  public partial class AccountAndMetadata : Migration
  {
    /// <inheritdoc />
    protected override void Up(MigrationBuilder migrationBuilder)
    {
      migrationBuilder.CreateTable(
          name: "Account",
          columns: table => new
          {
            Id = table.Column<Guid>(type: "TEXT", nullable: false),
            UserId = table.Column<Guid>(type: "TEXT", nullable: false),
            InstituionId = table.Column<Guid>(type: "TEXT", nullable: false),
            Name = table.Column<string>(type: "TEXT", nullable: false),
            CreatedAt = table.Column<DateTimeOffset>(type: "TEXT", nullable: false),
            UpdatedAt = table.Column<DateTimeOffset>(type: "TEXT", nullable: false)
          },
          constraints: table =>
          {
            table.PrimaryKey("PK_Account", x => x.Id);
            table.ForeignKey(
                      name: "FK_Account_Institution_InstituionId",
                      column: x => x.InstituionId,
                      principalTable: "Institution",
                      principalColumn: "Id",
                      onDelete: ReferentialAction.Cascade);
            table.ForeignKey(
                      name: "FK_Account_Users_UserId",
                      column: x => x.UserId,
                      principalTable: "Users",
                      principalColumn: "Id",
                      onDelete: ReferentialAction.Cascade);
          });

      migrationBuilder.CreateTable(
          name: "AccountMetadata",
          columns: table => new
          {
            Id = table.Column<Guid>(type: "TEXT", nullable: false),
            Type = table.Column<string>(type: "TEXT", maxLength: 21, nullable: false),
            AccountId = table.Column<Guid>(type: "TEXT", nullable: false),
            PlaidId = table.Column<string>(type: "TEXT", nullable: true),
            PlaidName = table.Column<string>(type: "TEXT", nullable: true),
            CreatedAt = table.Column<DateTimeOffset>(type: "TEXT", nullable: false),
            UpdatedAt = table.Column<DateTimeOffset>(type: "TEXT", nullable: false)
          },
          constraints: table =>
          {
            table.PrimaryKey("PK_AccountMetadata", x => x.Id);
            table.ForeignKey(
                      name: "FK_AccountMetadata_Account_AccountId",
                      column: x => x.AccountId,
                      principalTable: "Account",
                      principalColumn: "Id",
                      onDelete: ReferentialAction.Cascade);
          });

      migrationBuilder.CreateIndex(
          name: "IX_Account_InstituionId",
          table: "Account",
          column: "InstituionId");

      migrationBuilder.CreateIndex(
          name: "IX_Account_UserId",
          table: "Account",
          column: "UserId");

      migrationBuilder.CreateIndex(
          name: "IX_AccountMetadata_AccountId",
          table: "AccountMetadata",
          column: "AccountId",
          unique: true);
    }

    /// <inheritdoc />
    protected override void Down(MigrationBuilder migrationBuilder)
    {
      migrationBuilder.DropTable(
          name: "AccountMetadata");

      migrationBuilder.DropTable(
          name: "Account");
    }
  }
}