using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using PSTW_CentralSystem.Models; // Add this to use the MarineTarball class

namespace PSTW_CentralSystem.DBContext
{
    public class MMSSystemContext : DbContext
    {
        public MMSSystemContext(DbContextOptions<MMSSystemContext> options) : base(options)
        {
        }

        // DbSet for tbl_marine_tarball
        public DbSet<MarineTarball> MarineTarballs { get; set; }
        public DbSet<MarineStation> MarineStations { get; set; }
        public DbSet<State> States { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<Level> Levels { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // Configure properties if needed
            modelBuilder.Entity<MarineTarball>(entity =>
            {
                entity.ToTable("tbl_marine_tarball");

                entity.HasKey(e => e.Id); // Primary key
                entity.Property(e => e.Id).HasColumnName("id");
                entity.Property(e => e.ReportID).HasColumnName("reportID").HasMaxLength(50);
                entity.Property(e => e.FirstSampler).HasColumnName("firstSampler").HasMaxLength(50);
                entity.Property(e => e.SecondSampler).HasColumnName("secondSampler").HasMaxLength(50);
                entity.Property(e => e.DateSample).HasColumnName("dateSample");
                entity.Property(e => e.TimeSample).HasColumnName("timeSample");
                entity.Property(e => e.StationID).HasColumnName("stationID").HasMaxLength(20);
                entity.Property(e => e.ClassifyID).HasColumnName("classifyID").HasMaxLength(20);
                entity.Property(e => e.Latitude).HasColumnName("latitude");
                entity.Property(e => e.Longitude).HasColumnName("longitude");
                entity.Property(e => e.GetLatitude).HasColumnName("getLatitude");
                entity.Property(e => e.GetLongitude).HasColumnName("getLongitude");
                entity.Property(e => e.OptionalName1).HasColumnName("optionalName1");
                entity.Property(e => e.OptionalName2).HasColumnName("optionalName2");
                entity.Property(e => e.OptionalName3).HasColumnName("optionalName3");
                entity.Property(e => e.OptionalName4).HasColumnName("optionalName4");
                entity.Property(e => e.Timestamp).HasColumnName("timestamp");

                entity.HasOne(t => t.User)
                  .WithMany()
                  .HasForeignKey(t => t.FirstSampler)
                  .HasPrincipalKey(u => u.FullName); // Assuming User has FullName

                //Configure relationship with TarballStation
                entity.HasOne(m => m.MarineStation)
                    .WithMany()
                    .HasForeignKey(m => m.StationID)
                    .HasPrincipalKey(t => t.StationID);
            });

            modelBuilder.Entity<MarineStation>(entity =>
            {
                entity.ToTable("tbl_marine_station");
                entity.HasKey(e => e.Id); // Primary key
                entity.Property(e => e.Id).HasColumnName("id");
                entity.Property(e => e.StationID).HasColumnName("stationID").HasMaxLength(20);
                entity.Property(e => e.StateID).HasColumnName("stateID").HasMaxLength(10);
                entity.Property(e => e.CategoryID).HasColumnName("categoryID").HasMaxLength(10);
                entity.Property(e => e.LocationName).HasColumnName("locationName").HasMaxLength(50);
                entity.Property(e => e.Longitude).HasColumnName("longitude").HasColumnType("decimal(10,5)");
                entity.Property(e => e.Latitude).HasColumnName("latitude").HasColumnType("decimal(10,5)");

                // Configure relationship with State
                entity.HasOne(t => t.State)
                      .WithMany()
                      .HasForeignKey(t => t.StateID)
                      .HasPrincipalKey(s => s.StateID);
            });

            modelBuilder.Entity<State>(entity =>
            {
                entity.ToTable("tbl_state");
                entity.HasKey(e => e.Id); // Primary key
                entity.Property(e => e.Id).HasColumnName("id");
                entity.Property(e => e.StateID).HasColumnName("stateID").HasMaxLength(20);
                entity.Property(e => e.StateName).HasColumnName("stateName").HasMaxLength(50);
            });

            modelBuilder.Entity<User>(entity =>
            {
                entity.ToTable("tbl_user");
                entity.HasKey(e => e.Id); // Primary key
                entity.Property(e => e.Id).HasColumnName("id");
                entity.Property(e => e.UserID).HasColumnName("userID").HasMaxLength(20);
                entity.Property(e => e.FullName).HasColumnName("fullname").HasMaxLength(50);
                entity.Property(e => e.Username).HasColumnName("username").HasMaxLength(40);
                entity.Property(e => e.Password).HasColumnName("pwd").HasMaxLength(100);
                entity.Property(e => e.LevelID).HasColumnName("levelID").HasMaxLength(10);
                entity.Property(e => e.DeptID).HasColumnName("departID").HasMaxLength(10);

                entity.HasOne(u => u.Level)
                  .WithMany()
                  .HasForeignKey(u => u.LevelID)
                  .HasPrincipalKey(l => l.LevelID);
            });

            modelBuilder.Entity<Level>().ToTable("tbl_level");

            modelBuilder.Entity<Level>(entity =>
            {
                entity.HasKey(e => e.Id); // Primary key
                entity.Property(e => e.Id).HasColumnName("id");
                entity.Property(e => e.LevelID).HasColumnName("levelID").HasMaxLength(10);
                entity.Property(e => e.LevelName).HasColumnName("levelName").HasMaxLength(10);
            });
        }
    }
}
