using Microsoft.EntityFrameworkCore;
using Forge.Core.Models;

namespace Forge.Data;

public class ForgeDbContext : DbContext
{
    public DbSet<Repository> Repositories => Set<Repository>();
    public DbSet<PasskeyCredential> PasskeyCredentials => Set<PasskeyCredential>();
    
    public ForgeDbContext(DbContextOptions<ForgeDbContext> options) : base(options)
    {
    }
    
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Repository>(entity =>
        {
            entity.HasKey(r => r.Id);
            entity.HasIndex(r => new { r.Owner, r.Name }).IsUnique();
            entity.Property(r => r.Name).IsRequired().HasMaxLength(100);
            entity.Property(r => r.Owner).IsRequired().HasMaxLength(100);
            entity.Property(r => r.Description).HasMaxLength(500);
            entity.Property(r => r.Path).IsRequired().HasMaxLength(500);
            entity.Property(r => r.DefaultBranch).IsRequired().HasMaxLength(100);
        });
        
        modelBuilder.Entity<PasskeyCredential>(entity =>
        {
            entity.HasKey(c => c.Id);
            entity.HasIndex(c => c.CredentialId).IsUnique();
            entity.HasIndex(c => c.Username);
            entity.Property(c => c.Username).IsRequired().HasMaxLength(100);
            entity.Property(c => c.Name).HasMaxLength(100);
            entity.Ignore(c => c.AaGuid); // Don't persist AaGuid, it's only needed during registration
        });
    }
}
An unhandled error has occurred. Reload 🗙