// <auto-generated />
using BTCPayServer.Plugins.MicroNode;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace BTCPayServer.Plugins.MicroNode.Migrations
{
    [DbContext(typeof(MicroNodeContext))]
    [Migration("20240115112915_Initial")]
    partial class Initial
    {
        /// <inheritdoc />
        protected override void BuildTargetModel(ModelBuilder modelBuilder)
        {
#pragma warning disable 612, 618
            modelBuilder
                .HasDefaultSchema("BTCPayServer.Plugins.MicroNode")
                .HasAnnotation("ProductVersion", "8.0.0")
                .HasAnnotation("Relational:MaxIdentifierLength", 63);

            NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);

            modelBuilder.Entity("BTCPayServer.Plugins.MicroNode.MicroAccount", b =>
                {
                    b.Property<string>("Key")
                        .HasColumnType("text");

                    b.Property<long>("Balance")
                        .HasColumnType("bigint");

                    b.Property<long>("BalanceCheckpoint")
                        .HasColumnType("bigint");

                    b.Property<string>("MasterStoreId")
                        .HasColumnType("text");

                    b.HasKey("Key");

                    b.ToTable("MicroAccounts", "BTCPayServer.Plugins.MicroNode");
                });

            modelBuilder.Entity("BTCPayServer.Plugins.MicroNode.MicroTransaction", b =>
                {
                    b.Property<string>("Id")
                        .HasColumnType("text");

                    b.Property<string>("AccountId")
                        .HasColumnType("text");

                    b.Property<bool>("Accounted")
                        .HasColumnType("boolean");

                    b.Property<bool>("Active")
                        .HasColumnType("boolean");

                    b.Property<long>("Amount")
                        .HasColumnType("bigint");

                    b.Property<string>("DependentAccountId")
                        .HasColumnType("text");

                    b.Property<string>("DependentId")
                        .HasColumnType("text");

                    b.Property<string>("Type")
                        .HasColumnType("text");

                    b.HasKey("Id", "AccountId");

                    b.HasIndex("AccountId");

                    b.HasIndex("DependentId", "AccountId");

                    b.HasIndex("DependentId", "DependentAccountId");

                    b.ToTable("MicroTransactions", "BTCPayServer.Plugins.MicroNode", t =>
                        {
                            t.HasTrigger("LC_TRIGGER_AFTER_DELETE_MICROTRANSACTION");

                            t.HasTrigger("LC_TRIGGER_AFTER_INSERT_MICROTRANSACTION");

                            t.HasTrigger("LC_TRIGGER_AFTER_UPDATE_MICROTRANSACTION");
                        });

                    b
                        .HasAnnotation("LC_TRIGGER_AFTER_DELETE_MICROTRANSACTION", "CREATE FUNCTION \"BTCPayServer.Plugins.MicroNode\".\"LC_TRIGGER_AFTER_DELETE_MICROTRANSACTION\"() RETURNS trigger as $LC_TRIGGER_AFTER_DELETE_MICROTRANSACTION$\r\nBEGIN\r\n  \r\n  IF OLD.\"Accounted\" IS TRUE THEN \r\n    UPDATE \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\"\r\n    SET \"Balance\" = \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\".\"Balance\" - OLD.\"Amount\"\r\n    WHERE \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\".\"Key\" = OLD.\"AccountId\";\r\n  END IF;\r\n  UPDATE \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\"\r\n  SET \"BalanceCheckpoint\" = \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\".\"BalanceCheckpoint\" + 1\r\n  WHERE \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\".\"Key\" = OLD.\"AccountId\";\r\nRETURN OLD;\r\nEND;\r\n$LC_TRIGGER_AFTER_DELETE_MICROTRANSACTION$ LANGUAGE plpgsql;\r\nCREATE TRIGGER LC_TRIGGER_AFTER_DELETE_MICROTRANSACTION AFTER DELETE\r\nON \"BTCPayServer.Plugins.MicroNode\".\"MicroTransactions\"\r\nFOR EACH ROW EXECUTE PROCEDURE \"BTCPayServer.Plugins.MicroNode\".\"LC_TRIGGER_AFTER_DELETE_MICROTRANSACTION\"();")
                        .HasAnnotation("LC_TRIGGER_AFTER_INSERT_MICROTRANSACTION", "CREATE FUNCTION \"BTCPayServer.Plugins.MicroNode\".\"LC_TRIGGER_AFTER_INSERT_MICROTRANSACTION\"() RETURNS trigger as $LC_TRIGGER_AFTER_INSERT_MICROTRANSACTION$\r\nBEGIN\r\n  \r\n  IF NEW.\"Accounted\" IS TRUE THEN \r\n    UPDATE \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\"\r\n    SET \"Balance\" = \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\".\"Balance\" + NEW.\"Amount\"\r\n    WHERE \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\".\"Key\" = NEW.\"AccountId\";\r\n  END IF;\r\n  UPDATE \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\"\r\n  SET \"BalanceCheckpoint\" = \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\".\"BalanceCheckpoint\" + 1\r\n  WHERE \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\".\"Key\" = NEW.\"AccountId\";\r\n  UPDATE \"BTCPayServer.Plugins.MicroNode\".\"MicroTransactions\"\r\n  SET \"Accounted\" = NEW.\"Accounted\", \"Active\" = NEW.\"Active\"\r\n  WHERE NEW.\"Id\" = \"BTCPayServer.Plugins.MicroNode\".\"MicroTransactions\".\"DependentId\" AND \"BTCPayServer.Plugins.MicroNode\".\"MicroTransactions\".\"AccountId\" = NEW.\"AccountId\";\r\nRETURN NEW;\r\nEND;\r\n$LC_TRIGGER_AFTER_INSERT_MICROTRANSACTION$ LANGUAGE plpgsql;\r\nCREATE TRIGGER LC_TRIGGER_AFTER_INSERT_MICROTRANSACTION AFTER INSERT\r\nON \"BTCPayServer.Plugins.MicroNode\".\"MicroTransactions\"\r\nFOR EACH ROW EXECUTE PROCEDURE \"BTCPayServer.Plugins.MicroNode\".\"LC_TRIGGER_AFTER_INSERT_MICROTRANSACTION\"();")
                        .HasAnnotation("LC_TRIGGER_AFTER_UPDATE_MICROTRANSACTION", "CREATE FUNCTION \"BTCPayServer.Plugins.MicroNode\".\"LC_TRIGGER_AFTER_UPDATE_MICROTRANSACTION\"() RETURNS trigger as $LC_TRIGGER_AFTER_UPDATE_MICROTRANSACTION$\r\nBEGIN\r\n  UPDATE \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\"\r\n  SET \"BalanceCheckpoint\" = \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\".\"BalanceCheckpoint\" + 1\r\n  WHERE \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\".\"Key\" = OLD.\"AccountId\";\r\n  UPDATE \"BTCPayServer.Plugins.MicroNode\".\"MicroTransactions\"\r\n  SET \"Accounted\" = NEW.\"Accounted\", \"Active\" = NEW.\"Active\", \"DependentId\" = NEW.\"Id\"\r\n  WHERE OLD.\"Id\" = \"BTCPayServer.Plugins.MicroNode\".\"MicroTransactions\".\"DependentId\" AND \"BTCPayServer.Plugins.MicroNode\".\"MicroTransactions\".\"AccountId\" = NEW.\"AccountId\";\r\n  \r\n  IF NEW.\"Accounted\" IS TRUE AND OLD.\"Accounted\" IS FALSE THEN \r\n    UPDATE \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\"\r\n    SET \"Balance\" = \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\".\"Balance\" + NEW.\"Amount\"\r\n    WHERE \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\".\"Key\" = OLD.\"AccountId\";\r\n  END IF;\r\n  \r\n  IF NEW.\"Accounted\" IS TRUE AND OLD.\"Accounted\" IS TRUE AND OLD.\"Amount\" <> NEW.\"Amount\" THEN \r\n    UPDATE \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\"\r\n    SET \"Balance\" = \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\".\"Balance\" - OLD.\"Amount\" + NEW.\"Amount\"\r\n    WHERE \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\".\"Key\" = OLD.\"AccountId\";\r\n  END IF;\r\n  \r\n  IF NEW.\"Accounted\" IS FALSE AND OLD.\"Accounted\" IS TRUE THEN \r\n    UPDATE \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\"\r\n    SET \"Balance\" = \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\".\"Balance\" - OLD.\"Amount\"\r\n    WHERE \"BTCPayServer.Plugins.MicroNode\".\"MicroAccounts\".\"Key\" = OLD.\"AccountId\";\r\n  END IF;\r\nRETURN NEW;\r\nEND;\r\n$LC_TRIGGER_AFTER_UPDATE_MICROTRANSACTION$ LANGUAGE plpgsql;\r\nCREATE TRIGGER LC_TRIGGER_AFTER_UPDATE_MICROTRANSACTION AFTER UPDATE\r\nON \"BTCPayServer.Plugins.MicroNode\".\"MicroTransactions\"\r\nFOR EACH ROW EXECUTE PROCEDURE \"BTCPayServer.Plugins.MicroNode\".\"LC_TRIGGER_AFTER_UPDATE_MICROTRANSACTION\"();");
                });

            modelBuilder.Entity("BTCPayServer.Plugins.MicroNode.MicroTransaction", b =>
                {
                    b.HasOne("BTCPayServer.Plugins.MicroNode.MicroAccount", "Account")
                        .WithMany("Transactions")
                        .HasForeignKey("AccountId")
                        .OnDelete(DeleteBehavior.Cascade)
                        .IsRequired();

                    b.HasOne("BTCPayServer.Plugins.MicroNode.MicroTransaction", null)
                        .WithMany("Dependents")
                        .HasForeignKey("DependentId", "AccountId")
                        .OnDelete(DeleteBehavior.Cascade);

                    b.HasOne("BTCPayServer.Plugins.MicroNode.MicroTransaction", "Dependent")
                        .WithMany()
                        .HasForeignKey("DependentId", "DependentAccountId");

                    b.Navigation("Account");

                    b.Navigation("Dependent");
                });

            modelBuilder.Entity("BTCPayServer.Plugins.MicroNode.MicroAccount", b =>
                {
                    b.Navigation("Transactions");
                });

            modelBuilder.Entity("BTCPayServer.Plugins.MicroNode.MicroTransaction", b =>
                {
                    b.Navigation("Dependents");
                });
#pragma warning restore 612, 618
        }
    }
}
