using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace StockFin.Models;

public partial class FinancesContext : DbContext
{
    public FinancesContext()
    {
    }

    public FinancesContext(DbContextOptions<FinancesContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Account> Accounts { get; set; }

    public virtual DbSet<AccountType> AccountTypes { get; set; }

    public virtual DbSet<Increase> Increases { get; set; }

    public virtual DbSet<Patrimony> Patrimonies { get; set; }

    public virtual DbSet<Statement> Statements { get; set; }

    public virtual DbSet<Stock> Stocks { get; set; }

    public virtual DbSet<TimeValue> TimeValues { get; set; }

    public virtual DbSet<Transaction> Transactions { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see https://go.microsoft.com/fwlink/?LinkId=723263.
        => optionsBuilder.UseSqlServer("Server=localhost;Database=Finances;Trusted_Connection=True;TrustServerCertificate=True");

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Account>(entity =>
        {
            entity.Property(e => e.Bank).HasMaxLength(50);
            entity.Property(e => e.Title).HasMaxLength(50);

            entity.HasOne(d => d.Increase).WithMany(p => p.Accounts)
                .HasForeignKey(d => d.IncreaseId)
                .HasConstraintName("FK_Accounts_Increases");

            entity.HasOne(d => d.Type).WithMany(p => p.Accounts)
                .HasForeignKey(d => d.TypeId)
                .HasConstraintName("FK_Accounts_AccountTypes");
        });

        modelBuilder.Entity<AccountType>(entity =>
        {
            entity.Property(e => e.Title).HasMaxLength(50);
        });

        modelBuilder.Entity<Increase>(entity =>
        {
            entity.Property(e => e.Title).HasMaxLength(50);
        });

        modelBuilder.Entity<Patrimony>(entity =>
        {
            entity.ToTable("Patrimony");

            entity.Property(e => e.Title).HasMaxLength(50);

            entity.HasOne(d => d.Account).WithMany(p => p.Patrimonies)
                .HasForeignKey(d => d.AccountId)
                .HasConstraintName("FK_Patrimony_Accounts");
        });

        modelBuilder.Entity<Statement>(entity =>
        {
            entity.Property(e => e.Date).HasColumnType("datetime");

            entity.HasOne(d => d.Account).WithMany(p => p.Statements)
                .HasForeignKey(d => d.AccountId)
                .HasConstraintName("FK_Statements_Accounts");
        });

        modelBuilder.Entity<Stock>(entity =>
        {
            entity.Property(e => e.Isin)
                .HasMaxLength(50)
                .HasColumnName("ISIN");
            entity.Property(e => e.Ticker).HasMaxLength(50);
            entity.Property(e => e.Title).HasMaxLength(150);
        });

        modelBuilder.Entity<TimeValue>(entity =>
        {
            entity.Property(e => e.Date).HasColumnType("datetime");

            entity.HasOne(d => d.Stocks).WithMany(p => p.TimeValues)
                .HasForeignKey(d => d.StocksId)
                .HasConstraintName("FK_TimeValues_Stocks");
        });

        modelBuilder.Entity<Transaction>(entity =>
        {
            entity.Property(e => e.Date).HasColumnType("datetime");

            entity.HasOne(d => d.Account).WithMany(p => p.Transactions)
                .HasForeignKey(d => d.AccountId)
                .HasConstraintName("FK_Transactions_Accounts");

            entity.HasOne(d => d.Stocks).WithMany(p => p.Transactions)
                .HasForeignKey(d => d.StocksId)
                .HasConstraintName("FK_Transactions_Stocks");
        });

        OnModelCreatingPartial(modelBuilder);
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
