using Lactose.Models;

namespace Lactose.Context;

public class LactoseDbContext : DbContext {
    public DbSet<Asset>  Assets { get; set; }
    public DbSet<Album>  Albums { get; set; }
    public DbSet<Face>   Faces  { get; set; }
    public DbSet<Person> People { get; set; }
    public DbSet<User>   Users  { get; set; }
    public DbSet<Tag>    Tags   { get; set; }
    public DbSet<Folder> Folders { get; set; }
    public DbSet<Setting> Settings { get; set; }

    public LactoseDbContext(DbContextOptions options) : base(options) {}

    protected override void OnModelCreating(ModelBuilder modelBuilder) {
        //Album Relationships
        modelBuilder.Entity<Album>().HasOne(e => e.UserOwner).WithMany(e => e.OwnedAlbums);
        modelBuilder.Entity<Album>().HasOne(e => e.PersonOwner).WithMany(e => e.Albums);
        modelBuilder.Entity<Album>().HasMany(e => e.Assets).WithMany(e => e.Albums);
        //Asset Relationships
        modelBuilder.Entity<Asset>().HasOne(e => e.Owner).WithMany(e => e.OwnedAssets);
        modelBuilder.Entity<Asset>().HasMany(e => e.SharedWith);
        modelBuilder.Entity<Asset>().HasMany(e => e.Albums).WithMany(e => e.Assets);
        modelBuilder.Entity<Asset>().HasMany(e => e.Tags).WithMany(e => e.Assets);
        modelBuilder.Entity<Asset>().HasMany(e => e.Faces).WithOne(e => e.Asset);
        modelBuilder.Entity<Asset>().HasOne(e => e.Folder).WithMany(e => e.Assets);
        //Face Relationships
        modelBuilder.Entity<Face>().HasOne(e => e.Asset).WithMany(e => e.Faces);
        modelBuilder.Entity<Face>().HasOne(e => e.Person).WithMany(e => e.Faces);
        //Person Relationships
        modelBuilder.Entity<Person>().HasMany(e => e.Faces).WithOne(e => e.Person);
        modelBuilder.Entity<Person>().HasMany(e => e.Albums).WithOne(e => e.PersonOwner);
        //Tag Relationships
        modelBuilder.Entity<Tag>().HasOne(e => e.Parent);
        modelBuilder.Entity<Tag>().HasMany(e => e.Assets).WithMany(e => e.Tags);
        //User Relationships
        modelBuilder.Entity<User>().HasMany(e => e.OwnedAssets).WithOne(e => e.Owner);
        modelBuilder.Entity<User>().HasMany(e => e.OwnedAlbums).WithOne(e => e.UserOwner);
        //Folder Relationships
        modelBuilder.Entity<Folder>().HasMany(e => e.Assets).WithOne(e => e.Folder);
    }
}
