using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace FiscalOS.Infra.Migrations
{
  /// <inheritdoc />
  public partial class InstitutionAndMetadata : Migration
  {
    /// <inheritdoc />
    protected override void Up(MigrationBuilder migrationBuilder)
    {
      migrationBuilder.CreateTable(
          name: "Institution",
          columns: table => new
          {
            Id = table.Column<Guid>(type: "TEXT", nullable: false),
            UserId = 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_Institution", x => x.Id);
            table.ForeignKey(
                      name: "FK_Institution_Users_UserId",
                      column: x => x.UserId,
                      principalTable: "Users",
                      principalColumn: "Id",
                      onDelete: ReferentialAction.Cascade);
          });

      migrationBuilder.CreateTable(
          name: "InstitutionMetadata",
          columns: table => new
          {
            Id = table.Column<Guid>(type: "TEXT", nullable: false),
            Type = table.Column<string>(type: "TEXT", maxLength: 21, nullable: false),
            InstitutionId = table.Column<Guid>(type: "TEXT", nullable: false),
            PlaidId = table.Column<string>(type: "TEXT", nullable: true),
            PlaidName = table.Column<string>(type: "TEXT", nullable: true),
            EncryptedAccessToken = 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_InstitutionMetadata", x => x.Id);
            table.ForeignKey(
                      name: "FK_InstitutionMetadata_Institution_InstitutionId",
                      column: x => x.InstitutionId,
                      principalTable: "Institution",
                      principalColumn: "Id",
                      onDelete: ReferentialAction.Cascade);
          });

      migrationBuilder.CreateIndex(
          name: "IX_Institution_UserId",
          table: "Institution",
          column: "UserId");

      migrationBuilder.CreateIndex(
          name: "IX_InstitutionMetadata_InstitutionId",
          table: "InstitutionMetadata",
          column: "InstitutionId",
          unique: true);
    }

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

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