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

namespace KinoPoisk
{
    public partial class KinoPoiskContext : DbContext
    {
        public KinoPoiskContext()
        {
        }

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

        public virtual DbSet<Film> Films { get; set; } = null!;
        public virtual DbSet<MovieSession> MovieSessions { get; set; } = null!;
        public virtual DbSet<Status> Statuses { get; set; } = null!;
        public virtual DbSet<Ticket> Tickets { get; set; } = null!;
        public virtual DbSet<User> Users { get; set; } = null!;

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                //#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 http://go.microsoft.com/fwlink/?LinkId=723263.
                optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB; Database=KinoPoisk");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Film>(entity =>
            {
                entity.Property(e => e.Discript).HasMaxLength(500);

                entity.Property(e => e.DurationFilm)
                    .HasMaxLength(10)
                    .IsUnicode(false);

                entity.Property(e => e.GanreFilm).HasMaxLength(200);

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

                entity.Property(e => e.YearFilm).HasColumnType("date");
            });

            modelBuilder.Entity<MovieSession>(entity =>
            {
                entity.Property(e => e.DatePremiers).HasColumnType("date");

                entity.Property(e => e.IdFilm).HasColumnName("Id_Film");

                entity.Property(e => e.Price).HasColumnType("decimal(18, 2)");

                entity.HasOne(d => d.IdFilmNavigation)
                    .WithMany(p => p.MovieSessions)
                    .HasForeignKey(d => d.IdFilm)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_MovieSessions_Films");
            });

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

            modelBuilder.Entity<Ticket>(entity =>
            {
                entity.Property(e => e.DatePurchase).HasColumnType("date");

                entity.Property(e => e.IdFilm).HasColumnName("Id_Film");

                entity.Property(e => e.IdOwner).HasColumnName("Id_Owner");

                entity.HasOne(d => d.IdFilmNavigation)
                    .WithMany(p => p.Tickets)
                    .HasForeignKey(d => d.IdFilm)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_Tickets_Films");

                entity.HasOne(d => d.IdOwnerNavigation)
                    .WithMany(p => p.Tickets)
                    .HasForeignKey(d => d.IdOwner)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_Tickets_Users");
            });

            modelBuilder.Entity<User>(entity =>
            {
                entity.Property(e => e.FirstName).HasMaxLength(100);

                entity.Property(e => e.LastName).HasMaxLength(100);

                entity.Property(e => e.PhoneNumber)
                    .HasMaxLength(15)
                    .IsUnicode(false);

                entity.Property(e => e.UserPassword)
                    .HasMaxLength(100)
                    .IsUnicode(false);

                entity.HasOne(d => d.StatusNavigation)
                    .WithMany(p => p.Users)
                    .HasForeignKey(d => d.Status)
                    .HasConstraintName("FK_Users_Statuses");
            });

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
}
