diff --git a/src/Heimdall.Api/Controllers/PersonsController.cs b/src/Heimdall.Api/Controllers/PersonsController.cs new file mode 100644 index 0000000..e03b8cc --- /dev/null +++ b/src/Heimdall.Api/Controllers/PersonsController.cs @@ -0,0 +1,82 @@ +using Heimdall.Application.DTOs; +using Heimdall.Application.Services; +using Heimdall.Infrastructure.Security; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.RateLimiting; + +namespace Heimdall.Api.Controllers; + +[ApiController] +[Route("api/persons")] +[EnableRateLimiting("Api")] +public class PersonsController : ControllerBase +{ + private readonly IPersonService _personService; + + public PersonsController(IPersonService personService) => _personService = personService; + + [Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.PersonWrite)] + [HttpPost] + public async Task Create([FromBody] CreatePersonRequest request, CancellationToken ct) + { + var result = await _personService.CreateAsync(request, ct); + return result.IsSuccess ? Ok(result.Data) : StatusCode(result.StatusCode ?? 400, new { error = result.Error }); + } + + [Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.PersonWrite)] + [HttpPut("{id:guid}")] + public async Task Update(Guid id, [FromBody] UpdatePersonRequest request, CancellationToken ct) + { + var result = await _personService.UpdateAsync(id, request, ct); + return result.IsSuccess ? Ok(result.Data) : StatusCode(result.StatusCode ?? 404, new { error = result.Error }); + } + + [Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.PersonRead)] + [HttpGet("{id:guid}")] + public async Task GetById(Guid id, CancellationToken ct) + { + var result = await _personService.GetByIdAsync(id, ct); + return result.IsSuccess ? Ok(result.Data) : StatusCode(result.StatusCode ?? 404, new { error = result.Error }); + } + + [Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.PersonRead)] + [HttpGet] + public async Task GetAll([FromQuery] string? category, CancellationToken ct) + { + var result = await _personService.GetAllAsync(category, ct); + return Ok(result.Data); + } + + [Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.PersonWrite)] + [HttpDelete("{id:guid}")] + public async Task Delete(Guid id, CancellationToken ct) + { + var result = await _personService.DeleteAsync(id, ct); + return result.IsSuccess ? Ok() : StatusCode(result.StatusCode ?? 404, new { error = result.Error }); + } + + [Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.PersonWrite)] + [HttpPost("{personId:guid}/faces")] + public async Task RegisterFace(Guid personId, [FromBody] RegisterFaceRequest request, CancellationToken ct) + { + var result = await _personService.RegisterFaceAsync(request with { PersonId = personId }, ct); + return result.IsSuccess ? Ok(result.Data) : StatusCode(result.StatusCode ?? 400, new { error = result.Error }); + } + + [Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.PersonRead)] + [HttpGet("{personId:guid}/faces")] + public async Task GetFaces(Guid personId, CancellationToken ct) + { + var result = await _personService.GetFacesAsync(personId, ct); + return Ok(result.Data); + } + + [Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.PersonWrite)] + [HttpDelete("faces/{faceId:guid}")] + public async Task DeleteFace(Guid faceId, CancellationToken ct) + { + var result = await _personService.DeleteFaceAsync(faceId, ct); + return result.IsSuccess ? Ok() : StatusCode(result.StatusCode ?? 404, new { error = result.Error }); + } +} diff --git a/src/Heimdall.Application/DTOs/PersonDto.cs b/src/Heimdall.Application/DTOs/PersonDto.cs new file mode 100644 index 0000000..9765942 --- /dev/null +++ b/src/Heimdall.Application/DTOs/PersonDto.cs @@ -0,0 +1,13 @@ +namespace Heimdall.Application.DTOs; + +public record PersonResponse( + Guid Id, string DisplayName, string? Notes, string? PhotoUrl, + string? Category, bool IsWatchlisted, int FaceCount, DateTime CreatedAt); + +public record CreatePersonRequest(string DisplayName, string? Category, string? Notes, bool IsWatchlisted = false); + +public record UpdatePersonRequest(string DisplayName, string? Notes, string? Category, bool IsWatchlisted); + +public record KnownFaceResponse(Guid Id, Guid PersonId, string? ThumbnailUrl, string? SourceEventId); + +public record RegisterFaceRequest(Guid PersonId, float[] Embedding, string? ThumbnailUrl = null, string? SourceEventId = null); diff --git a/src/Heimdall.Application/Services/IPersonService.cs b/src/Heimdall.Application/Services/IPersonService.cs new file mode 100644 index 0000000..2b129e5 --- /dev/null +++ b/src/Heimdall.Application/Services/IPersonService.cs @@ -0,0 +1,16 @@ +using Heimdall.Application.Common; +using Heimdall.Application.DTOs; + +namespace Heimdall.Application.Services; + +public interface IPersonService +{ + Task> CreateAsync(CreatePersonRequest request, CancellationToken ct); + Task> UpdateAsync(Guid id, UpdatePersonRequest request, CancellationToken ct); + Task> GetByIdAsync(Guid id, CancellationToken ct); + Task>> GetAllAsync(string? category, CancellationToken ct); + Task DeleteAsync(Guid id, CancellationToken ct); + Task> RegisterFaceAsync(RegisterFaceRequest request, CancellationToken ct); + Task>> GetFacesAsync(Guid personId, CancellationToken ct); + Task DeleteFaceAsync(Guid faceId, CancellationToken ct); +} diff --git a/src/Heimdall.Domain/Entities/Person.cs b/src/Heimdall.Domain/Entities/Person.cs new file mode 100644 index 0000000..e16640c --- /dev/null +++ b/src/Heimdall.Domain/Entities/Person.cs @@ -0,0 +1,70 @@ +using Heimdall.Domain.Primitives; + +namespace Heimdall.Domain.Entities; + +/// +/// A person of interest — known individual monitored by the system. +/// Can be a family member, employee, or person on a watchlist. +/// Associated with detection events via face recognition (EventType.FaceRecognized). +/// +public class Person : Entity, IAuditableEntity +{ + public string DisplayName { get; private set; } = string.Empty; + public string? Notes { get; private set; } + public string? PhotoUrl { get; private set; } // Main portrait photo + public string? Category { get; private set; } // family, employee, watchlist, unknown + public bool IsWatchlisted { get; private set; } + public DateTime CreatedAt { get; set; } + public DateTime UpdatedAt { get; set; } + + // Navigation + private readonly List _faces = []; + public IReadOnlyCollection Faces => _faces.AsReadOnly(); + + private Person() { } // EF Core + + public Person(string displayName, string? category = null) + : base(Guid.NewGuid()) + { + DisplayName = displayName; + Category = category; + } + + public void Update(string displayName, string? notes, string? category, bool isWatchlisted) + { + DisplayName = displayName; + Notes = notes; + Category = category; + IsWatchlisted = isWatchlisted; + } + + public void SetPhoto(string photoUrl) => PhotoUrl = photoUrl; + + public void AddFace(KnownFace face) => _faces.Add(face); +} + +/// +/// A registered face encoding for a known person. +/// Stored as a float array (128-d embedding from a face recognition model). +/// +public class KnownFace : Entity +{ + public Guid PersonId { get; private set; } + public string? ThumbnailUrl { get; private set; } + public float[] Embedding { get; private set; } = []; + public string? SourceEventId { get; private set; } + + // Navigation + public Person Person { get; private set; } = null!; + + private KnownFace() { } // EF Core + + public KnownFace(Guid personId, float[] embedding, string? thumbnailUrl = null, string? sourceEventId = null) + : base(Guid.NewGuid()) + { + PersonId = personId; + Embedding = embedding; + ThumbnailUrl = thumbnailUrl; + SourceEventId = sourceEventId; + } +} diff --git a/src/Heimdall.Infrastructure/Data/AppDbContext.cs b/src/Heimdall.Infrastructure/Data/AppDbContext.cs index 0ae1c64..2a6e6a0 100644 --- a/src/Heimdall.Infrastructure/Data/AppDbContext.cs +++ b/src/Heimdall.Infrastructure/Data/AppDbContext.cs @@ -35,6 +35,10 @@ public class AppDbContext : IdentityDbContext // Event entities public DbSet CameraEvents => Set(); + // Person entities + public DbSet Persons => Set(); + public DbSet KnownFaces => Set(); + protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); diff --git a/src/Heimdall.Infrastructure/Data/Configurations/KnownFaceConfiguration.cs b/src/Heimdall.Infrastructure/Data/Configurations/KnownFaceConfiguration.cs new file mode 100644 index 0000000..8b9883b --- /dev/null +++ b/src/Heimdall.Infrastructure/Data/Configurations/KnownFaceConfiguration.cs @@ -0,0 +1,35 @@ +using Heimdall.Domain.Entities; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using System.Text.Json; + +namespace Heimdall.Infrastructure.Data.Configurations; + +public class KnownFaceConfiguration : IEntityTypeConfiguration +{ + private static readonly JsonSerializerOptions JsonOptions = new(); + + public void Configure(EntityTypeBuilder builder) + { + builder.ToTable("known_faces"); + + builder.HasKey(f => f.Id); + + builder.Property(f => f.ThumbnailUrl) + .HasMaxLength(2000) + .IsUnicode(false); + + builder.Property(f => f.SourceEventId) + .HasMaxLength(100) + .IsUnicode(false); + + // Store float[] as JSONB + builder.Property(f => f.Embedding) + .HasColumnType("jsonb") + .HasConversion( + v => JsonSerializer.Serialize(v, JsonOptions), + v => JsonSerializer.Deserialize(v, JsonOptions) ?? Array.Empty()); + + builder.HasIndex(f => f.PersonId); + } +} diff --git a/src/Heimdall.Infrastructure/Data/Configurations/PersonConfiguration.cs b/src/Heimdall.Infrastructure/Data/Configurations/PersonConfiguration.cs new file mode 100644 index 0000000..97fb907 --- /dev/null +++ b/src/Heimdall.Infrastructure/Data/Configurations/PersonConfiguration.cs @@ -0,0 +1,40 @@ +using Heimdall.Domain.Entities; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace Heimdall.Infrastructure.Data.Configurations; + +public class PersonConfiguration : IEntityTypeConfiguration +{ + public void Configure(EntityTypeBuilder builder) + { + builder.ToTable("persons"); + + builder.HasKey(p => p.Id); + + builder.Property(p => p.DisplayName) + .HasMaxLength(200) + .IsRequired(); + + builder.Property(p => p.Notes) + .HasMaxLength(2000); + + builder.Property(p => p.PhotoUrl) + .HasMaxLength(2000) + .IsUnicode(false); + + builder.Property(p => p.Category) + .HasMaxLength(50) + .IsUnicode(false); + + builder.HasIndex(p => p.DisplayName); + builder.HasIndex(p => p.Category); + builder.HasIndex(p => p.IsWatchlisted); + + // OwnsMany for KnownFace (stored as JSONB or separate table) + builder.HasMany(p => p.Faces) + .WithOne(f => f.Person) + .HasForeignKey(f => f.PersonId) + .OnDelete(DeleteBehavior.Cascade); + } +} diff --git a/src/Heimdall.Infrastructure/DependencyInjection.cs b/src/Heimdall.Infrastructure/DependencyInjection.cs index 4e1fa2c..da8a388 100644 --- a/src/Heimdall.Infrastructure/DependencyInjection.cs +++ b/src/Heimdall.Infrastructure/DependencyInjection.cs @@ -98,6 +98,7 @@ public static class DependencyInjection // Event services services.AddScoped(); + services.AddScoped(); services.AddSingleton(); // HTTP client for snapshot uploads (no default timeout) diff --git a/src/Heimdall.Infrastructure/Migrations/20260719152833_AddPersonsAndKnownFaces.Designer.cs b/src/Heimdall.Infrastructure/Migrations/20260719152833_AddPersonsAndKnownFaces.Designer.cs new file mode 100644 index 0000000..90cb097 --- /dev/null +++ b/src/Heimdall.Infrastructure/Migrations/20260719152833_AddPersonsAndKnownFaces.Designer.cs @@ -0,0 +1,1548 @@ +// +using System; +using Heimdall.Infrastructure.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Heimdall.Infrastructure.Migrations +{ + [DbContext(typeof(AppDbContext))] + [Migration("20260719152833_AddPersonsAndKnownFaces")] + partial class AddPersonsAndKnownFaces + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "10.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("Heimdall.Domain.Entities.Agent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ApiKeyHash") + .HasMaxLength(128) + .IsUnicode(false) + .HasColumnType("character varying(128)"); + + b.Property("ApiKeyPrefix") + .HasMaxLength(8) + .IsUnicode(false) + .HasColumnType("character varying(8)"); + + b.Property("CpuCores") + .HasColumnType("integer"); + + b.Property("CreatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("timestamp with time zone") + .HasDefaultValueSql("NOW()"); + + b.Property("DeletedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("GpuType") + .IsRequired() + .HasMaxLength(20) + .IsUnicode(false) + .HasColumnType("character varying(20)"); + + b.Property("HasGpu") + .HasColumnType("boolean"); + + b.Property("Hostname") + .IsRequired() + .HasMaxLength(255) + .IsUnicode(false) + .HasColumnType("character varying(255)"); + + b.Property("IsDeleted") + .HasColumnType("boolean"); + + b.Property("LastHeartbeat") + .HasColumnType("timestamp with time zone"); + + b.Property("MemoryMb") + .HasColumnType("bigint"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .IsUnicode(false) + .HasColumnType("character varying(200)"); + + b.Property("PublicIp") + .HasMaxLength(45) + .IsUnicode(false) + .HasColumnType("character varying(45)"); + + b.Property("SiteId") + .HasColumnType("uuid"); + + b.Property("Status") + .IsRequired() + .HasMaxLength(20) + .IsUnicode(false) + .HasColumnType("character varying(20)"); + + b.Property("UpdatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("timestamp with time zone") + .HasDefaultValueSql("NOW()"); + + b.Property("Version") + .IsRequired() + .HasMaxLength(50) + .IsUnicode(false) + .HasColumnType("character varying(50)"); + + b.HasKey("Id"); + + b.HasIndex("LastHeartbeat") + .HasFilter("\"Status\" = 'Online'"); + + b.HasIndex("SiteId"); + + b.ToTable("agents", (string)null); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.AgentSetting", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AgentId") + .HasColumnType("uuid") + .HasColumnName("agent_id"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("created_at"); + + b.Property("Key") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("key"); + + b.Property("UpdatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("updated_at"); + + b.Property("Value") + .IsRequired() + .HasColumnType("text") + .HasColumnName("value"); + + b.HasKey("Id"); + + b.HasIndex("AgentId", "Key") + .IsUnique(); + + b.ToTable("agent_settings", (string)null); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.ApiKey", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("timestamp with time zone") + .HasDefaultValueSql("NOW()"); + + b.Property("CreatedByUserId") + .HasColumnType("uuid"); + + b.Property("Enabled") + .HasColumnType("boolean"); + + b.Property("ExpiresAt") + .HasColumnType("timestamp with time zone"); + + b.Property("KeyHash") + .IsRequired() + .HasMaxLength(128) + .IsUnicode(false) + .HasColumnType("character varying(128)"); + + b.Property("KeyPrefix") + .IsRequired() + .HasMaxLength(8) + .IsUnicode(false) + .HasColumnType("character varying(8)"); + + b.Property("LastUsedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("SiteId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("KeyHash"); + + b.HasIndex("KeyPrefix"); + + b.HasIndex("SiteId"); + + b.ToTable("api_keys", (string)null); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.AppRole", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Name") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("roles", (string)null); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.AppSetting", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AllowedValues") + .HasMaxLength(2000) + .HasColumnType("character varying(2000)") + .HasColumnName("allowed_values"); + + b.Property("Category") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("character varying(100)") + .HasColumnName("category"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("created_at"); + + b.Property("Description") + .HasMaxLength(500) + .HasColumnType("character varying(500)") + .HasColumnName("description"); + + b.Property("Key") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)") + .HasColumnName("key"); + + b.Property("UpdatedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("updated_at"); + + b.Property("Value") + .IsRequired() + .HasColumnType("text") + .HasColumnName("value"); + + b.Property("ValueType") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("character varying(50)") + .HasColumnName("value_type"); + + b.HasKey("Id"); + + b.HasIndex("Key") + .IsUnique(); + + b.ToTable("app_settings", (string)null); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.AppUser", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AccessFailedCount") + .HasColumnType("integer"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("CreatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("timestamp with time zone") + .HasDefaultValueSql("NOW()"); + + b.Property("DisplayName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)"); + + b.Property("Email") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("EmailConfirmed") + .HasColumnType("boolean"); + + b.Property("Language") + .IsRequired() + .ValueGeneratedOnAdd() + .HasMaxLength(10) + .IsUnicode(false) + .HasColumnType("character varying(10)") + .HasDefaultValue("fr"); + + b.Property("LockoutEnabled") + .HasColumnType("boolean"); + + b.Property("LockoutEnd") + .HasColumnType("timestamp with time zone"); + + b.Property("MustChangePassword") + .ValueGeneratedOnAdd() + .HasColumnType("boolean") + .HasDefaultValue(false); + + b.Property("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("PasswordHash") + .HasColumnType("text"); + + b.Property("PhoneNumber") + .HasColumnType("text"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("boolean"); + + b.Property("SecurityStamp") + .HasColumnType("text"); + + b.Property("Theme") + .ValueGeneratedOnAdd() + .HasMaxLength(20) + .IsUnicode(false) + .HasColumnType("character varying(20)") + .HasDefaultValue("Auto"); + + b.Property("TwoFactorEnabled") + .HasColumnType("boolean"); + + b.Property("UserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.ToTable("users", (string)null); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.AuditLog", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Action") + .IsRequired() + .HasMaxLength(100) + .IsUnicode(false) + .HasColumnType("character varying(100)"); + + b.Property("IpAddress") + .HasMaxLength(45) + .IsUnicode(false) + .HasColumnType("character varying(45)"); + + b.Property("NewValue") + .HasColumnType("text"); + + b.Property("OldValue") + .HasColumnType("text"); + + b.Property("ResourceId") + .HasMaxLength(100) + .IsUnicode(false) + .HasColumnType("character varying(100)"); + + b.Property("ResourceType") + .IsRequired() + .HasMaxLength(100) + .IsUnicode(false) + .HasColumnType("character varying(100)"); + + b.Property("Timestamp") + .ValueGeneratedOnAdd() + .HasColumnType("timestamp with time zone") + .HasDefaultValueSql("NOW()"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("Timestamp") + .IsDescending(); + + b.HasIndex("UserId"); + + b.HasIndex("ResourceType", "ResourceId", "Timestamp") + .IsDescending(false, false, true); + + b.ToTable("audit_logs", (string)null); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.Camera", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AgentId") + .HasColumnType("uuid"); + + b.Property("Capabilities") + .IsRequired() + .HasMaxLength(64) + .IsUnicode(false) + .HasColumnType("character varying(64)"); + + b.Property("CreatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("timestamp with time zone") + .HasDefaultValueSql("NOW()"); + + b.Property("DeletedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("DetectedType") + .IsRequired() + .HasMaxLength(20) + .IsUnicode(false) + .HasColumnType("character varying(20)"); + + b.Property("DiscoveryMethod") + .IsRequired() + .HasMaxLength(20) + .IsUnicode(false) + .HasColumnType("character varying(20)"); + + b.Property("HasAudio") + .HasColumnType("boolean"); + + b.Property("HasMotionDetection") + .HasColumnType("boolean"); + + b.Property("HasPtz") + .HasColumnType("boolean"); + + b.Property("HttpPort") + .HasColumnType("integer"); + + b.Property("IpAddress") + .HasMaxLength(45) + .HasColumnType("character varying(45)"); + + b.Property("IsDeleted") + .HasColumnType("boolean"); + + b.Property("LastDiscoveredAt") + .HasColumnType("timestamp with time zone"); + + b.Property("LastStreamTestAt") + .HasColumnType("timestamp with time zone"); + + b.Property("LastTestedCodecs") + .HasMaxLength(200) + .IsUnicode(false) + .HasColumnType("character varying(200)"); + + b.Property("LastTestedHeight") + .HasColumnType("integer"); + + b.Property("LastTestedLatencyMs") + .HasColumnType("bigint"); + + b.Property("LastTestedWidth") + .HasColumnType("integer"); + + b.Property("MacAddress") + .HasMaxLength(17) + .HasColumnType("character varying(17)"); + + b.Property("Manufacturer") + .HasMaxLength(100) + .HasColumnType("character varying(100)"); + + b.Property("Model") + .HasMaxLength(100) + .HasColumnType("character varying(100)"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .IsUnicode(false) + .HasColumnType("character varying(200)"); + + b.Property("OnvifPort") + .HasColumnType("integer"); + + b.Property("RtspPort") + .HasColumnType("integer"); + + b.Property("RtspUrl") + .HasMaxLength(1000) + .IsUnicode(false) + .HasColumnType("character varying(1000)"); + + b.Property("SiteId") + .HasColumnType("uuid"); + + b.Property("Status") + .IsRequired() + .HasMaxLength(20) + .IsUnicode(false) + .HasColumnType("character varying(20)"); + + b.Property("SubRtspUrl") + .HasMaxLength(1000) + .IsUnicode(false) + .HasColumnType("character varying(1000)"); + + b.Property("UpdatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("timestamp with time zone") + .HasDefaultValueSql("NOW()"); + + b.HasKey("Id"); + + b.HasIndex("AgentId"); + + b.HasIndex("IpAddress"); + + b.HasIndex("MacAddress"); + + b.HasIndex("SiteId"); + + b.ToTable("cameras", (string)null); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.CameraAssignment", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AgentId") + .HasColumnType("uuid"); + + b.Property("AssignedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("CameraId") + .HasColumnType("uuid"); + + b.Property("Reason") + .IsRequired() + .HasMaxLength(50) + .IsUnicode(false) + .HasColumnType("character varying(50)"); + + b.Property("ReleasedAt") + .HasColumnType("timestamp with time zone"); + + b.HasKey("Id"); + + b.HasIndex("AssignedAt"); + + b.HasIndex("AgentId", "ReleasedAt") + .HasFilter("\"ReleasedAt\" IS NULL"); + + b.HasIndex("CameraId", "ReleasedAt") + .IsUnique() + .HasFilter("\"ReleasedAt\" IS NULL"); + + b.ToTable("camera_assignments", (string)null); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.CameraCredentials", b => + { + b.Property("CameraId") + .HasColumnType("uuid"); + + b.Property("CreatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("timestamp with time zone") + .HasDefaultValueSql("NOW()"); + + b.Property("OnvifPassword") + .IsRequired() + .HasMaxLength(500) + .IsUnicode(false) + .HasColumnType("character varying(500)"); + + b.Property("OnvifUsername") + .IsRequired() + .ValueGeneratedOnAdd() + .HasMaxLength(100) + .IsUnicode(false) + .HasColumnType("character varying(100)") + .HasDefaultValue("admin"); + + b.Property("RtspPassword") + .IsRequired() + .HasMaxLength(500) + .IsUnicode(false) + .HasColumnType("character varying(500)"); + + b.Property("RtspUsername") + .IsRequired() + .HasMaxLength(100) + .IsUnicode(false) + .HasColumnType("character varying(100)"); + + b.Property("UpdatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("timestamp with time zone") + .HasDefaultValueSql("NOW()"); + + b.HasKey("CameraId"); + + b.ToTable("camera_credentials", (string)null); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.CameraDetectionProfile", b => + { + b.Property("CameraId") + .HasColumnType("uuid"); + + b.Property("ActiveIntervalMs") + .HasColumnType("integer"); + + b.Property("AnimalCooldownSec") + .HasColumnType("integer"); + + b.Property("DetectAnimal") + .HasColumnType("boolean"); + + b.Property("DetectMotion") + .HasColumnType("boolean"); + + b.Property("DetectPackage") + .HasColumnType("boolean"); + + b.Property("DetectPerson") + .HasColumnType("boolean"); + + b.Property("DetectVehicle") + .HasColumnType("boolean"); + + b.Property("FaceRecognition") + .HasColumnType("boolean"); + + b.Property("IdleIntervalMs") + .HasColumnType("integer"); + + b.Property("IoUThreshold") + .HasColumnType("double precision"); + + b.Property("LicensePlateRecognition") + .HasColumnType("boolean"); + + b.Property("MaxEventsPerMinute") + .HasColumnType("integer"); + + b.Property("MinConfidence") + .ValueGeneratedOnAdd() + .HasColumnType("double precision") + .HasDefaultValue(0.40000000000000002); + + b.Property("MotionIntervalMs") + .HasColumnType("integer"); + + b.Property("MotionThreshold") + .HasColumnType("double precision"); + + b.Property("PersonCooldownSec") + .HasColumnType("integer"); + + b.Property("RequireMotionForAI") + .HasColumnType("boolean"); + + b.Property("VehicleCooldownSec") + .HasColumnType("integer"); + + b.Property("YoloModel") + .IsRequired() + .ValueGeneratedOnAdd() + .HasMaxLength(50) + .IsUnicode(false) + .HasColumnType("character varying(50)") + .HasDefaultValue("yolov8n"); + + b.HasKey("CameraId"); + + b.ToTable("camera_detection_profiles", (string)null); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.CameraDiscoveryResult", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("DetectedType") + .IsRequired() + .HasMaxLength(20) + .IsUnicode(false) + .HasColumnType("character varying(20)"); + + b.Property("DiscoveryMethod") + .IsRequired() + .HasMaxLength(20) + .IsUnicode(false) + .HasColumnType("character varying(20)"); + + b.Property("HomeKitDeviceId") + .HasMaxLength(50) + .IsUnicode(false) + .HasColumnType("character varying(50)"); + + b.Property("HomeKitModel") + .HasMaxLength(200) + .IsUnicode(false) + .HasColumnType("character varying(200)"); + + b.Property("HomeKitName") + .HasMaxLength(200) + .IsUnicode(false) + .HasColumnType("character varying(200)"); + + b.Property("Hostname") + .HasMaxLength(255) + .IsUnicode(false) + .HasColumnType("character varying(255)"); + + b.Property("HttpPort") + .HasColumnType("integer"); + + b.Property("HttpServerHeader") + .HasMaxLength(200) + .IsUnicode(false) + .HasColumnType("character varying(200)"); + + b.Property("HttpStatusCode") + .HasColumnType("integer"); + + b.Property("IpAddress") + .IsRequired() + .HasMaxLength(45) + .IsUnicode(false) + .HasColumnType("character varying(45)"); + + b.Property("IsHomeKitCamera") + .HasColumnType("boolean"); + + b.Property("IsProcessed") + .HasColumnType("boolean"); + + b.Property("IsReachable") + .HasColumnType("boolean"); + + b.Property("MacAddress") + .HasMaxLength(17) + .IsUnicode(false) + .HasColumnType("character varying(17)"); + + b.Property("OnvifDeviceServiceUrl") + .HasMaxLength(500) + .IsUnicode(false) + .HasColumnType("character varying(500)"); + + b.Property("OnvifHardware") + .HasMaxLength(200) + .IsUnicode(false) + .HasColumnType("character varying(200)"); + + b.Property("OnvifName") + .HasMaxLength(200) + .IsUnicode(false) + .HasColumnType("character varying(200)"); + + b.Property("OnvifPort") + .HasColumnType("integer"); + + b.PrimitiveCollection("OpenPorts") + .IsRequired() + .IsUnicode(false) + .HasColumnType("integer[]"); + + b.Property("ProbedAt") + .ValueGeneratedOnAdd() + .HasColumnType("timestamp with time zone") + .HasDefaultValueSql("NOW()"); + + b.Property("SiteId") + .HasColumnType("uuid"); + + b.Property("Vendor") + .HasMaxLength(100) + .IsUnicode(false) + .HasColumnType("character varying(100)"); + + b.HasKey("Id"); + + b.HasIndex("IpAddress"); + + b.HasIndex("IsProcessed"); + + b.HasIndex("SiteId"); + + b.ToTable("camera_discovery_results", (string)null); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.CameraEvent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AgentId") + .HasColumnType("uuid"); + + b.Property("CameraId") + .HasColumnType("uuid"); + + b.Property("Confidence") + .ValueGeneratedOnAdd() + .HasColumnType("double precision") + .HasDefaultValue(0.0); + + b.Property("CreatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("timestamp with time zone") + .HasDefaultValueSql("NOW()"); + + b.Property("Label") + .HasMaxLength(200) + .IsUnicode(false) + .HasColumnType("character varying(200)"); + + b.Property("MetadataJson") + .HasColumnType("jsonb"); + + b.Property("OccurredAt") + .HasColumnType("timestamp with time zone"); + + b.Property("SnapshotThumbUrl") + .HasMaxLength(2000) + .IsUnicode(false) + .HasColumnType("character varying(2000)"); + + b.Property("SnapshotUrl") + .HasMaxLength(2000) + .IsUnicode(false) + .HasColumnType("character varying(2000)"); + + b.Property("Type") + .HasColumnType("integer"); + + b.Property("UpdatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("timestamp with time zone") + .HasDefaultValueSql("NOW()"); + + b.HasKey("Id"); + + b.HasIndex("OccurredAt"); + + b.HasIndex("CameraId", "OccurredAt") + .IsDescending(false, true); + + b.HasIndex("CameraId", "Type"); + + b.ToTable("camera_events", (string)null); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.DiscoveredStreamUrl", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.PrimitiveCollection("Codecs") + .IsRequired() + .IsUnicode(false) + .HasColumnType("text[]"); + + b.Property("DiscoveryResultId") + .HasColumnType("uuid"); + + b.Property("Height") + .HasColumnType("integer"); + + b.Property("IsAlive") + .HasColumnType("boolean"); + + b.Property("IsMainStream") + .HasColumnType("boolean"); + + b.Property("LatencyMs") + .HasColumnType("bigint"); + + b.Property("Protocol") + .IsRequired() + .HasMaxLength(20) + .IsUnicode(false) + .HasColumnType("character varying(20)"); + + b.Property("Url") + .IsRequired() + .HasMaxLength(2000) + .IsUnicode(false) + .HasColumnType("character varying(2000)"); + + b.Property("Width") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("DiscoveryResultId"); + + b.HasIndex("IsAlive"); + + b.HasIndex("IsMainStream"); + + b.ToTable("discovered_streams", (string)null); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.Group", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("timestamp with time zone") + .HasDefaultValueSql("NOW()"); + + b.Property("DeletedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("Description") + .HasMaxLength(1000) + .HasColumnType("character varying(1000)"); + + b.Property("IsDeleted") + .HasColumnType("boolean"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)"); + + b.Property("UpdatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("timestamp with time zone") + .HasDefaultValueSql("NOW()"); + + b.HasKey("Id"); + + b.HasIndex("IsDeleted") + .HasFilter("\"IsDeleted\" = false"); + + b.HasIndex("Name"); + + b.ToTable("groups", (string)null); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.KnownFace", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Embedding") + .IsRequired() + .HasColumnType("jsonb"); + + b.Property("PersonId") + .HasColumnType("uuid"); + + b.Property("SourceEventId") + .HasMaxLength(100) + .IsUnicode(false) + .HasColumnType("character varying(100)"); + + b.Property("ThumbnailUrl") + .HasMaxLength(2000) + .IsUnicode(false) + .HasColumnType("character varying(2000)"); + + b.HasKey("Id"); + + b.HasIndex("PersonId"); + + b.ToTable("known_faces", (string)null); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.Person", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Category") + .HasMaxLength(50) + .IsUnicode(false) + .HasColumnType("character varying(50)"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("DisplayName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)"); + + b.Property("IsWatchlisted") + .HasColumnType("boolean"); + + b.Property("Notes") + .HasMaxLength(2000) + .HasColumnType("character varying(2000)"); + + b.Property("PhotoUrl") + .HasMaxLength(2000) + .IsUnicode(false) + .HasColumnType("character varying(2000)"); + + b.Property("UpdatedAt") + .HasColumnType("timestamp with time zone"); + + b.HasKey("Id"); + + b.HasIndex("Category"); + + b.HasIndex("DisplayName"); + + b.HasIndex("IsWatchlisted"); + + b.ToTable("persons", (string)null); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.Site", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Address") + .HasMaxLength(500) + .HasColumnType("character varying(500)"); + + b.Property("CreatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("timestamp with time zone") + .HasDefaultValueSql("NOW()"); + + b.Property("DeletedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("Enabled") + .HasColumnType("boolean"); + + b.Property("IsDeleted") + .HasColumnType("boolean"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)"); + + b.Property("RegistrationTokenHash") + .HasMaxLength(128) + .IsUnicode(false) + .HasColumnType("character varying(128)"); + + b.Property("Timezone") + .IsRequired() + .ValueGeneratedOnAdd() + .HasMaxLength(100) + .IsUnicode(false) + .HasColumnType("character varying(100)") + .HasDefaultValue("UTC"); + + b.Property("TokenExpiresAt") + .HasColumnType("timestamp with time zone"); + + b.Property("UpdatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("timestamp with time zone") + .HasDefaultValueSql("NOW()"); + + b.HasKey("Id"); + + b.HasIndex("IsDeleted") + .HasFilter("\"IsDeleted\" = false"); + + b.HasIndex("Name"); + + b.ToTable("sites", (string)null); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.UserGroup", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("GroupId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "GroupId"); + + b.HasIndex("GroupId"); + + b.ToTable("user_groups", (string)null); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.UserSite", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("SiteId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "SiteId"); + + b.HasIndex("SiteId"); + + b.ToTable("user_sites", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("role_claims", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("user_claims", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("ProviderKey") + .HasColumnType("text"); + + b.Property("ProviderDisplayName") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("user_logins", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("user_roles", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("Value") + .HasColumnType("text"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("user_tokens", (string)null); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.Agent", b => + { + b.HasOne("Heimdall.Domain.Entities.Site", "Site") + .WithMany("Agents") + .HasForeignKey("SiteId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("Site"); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.AgentSetting", b => + { + b.HasOne("Heimdall.Domain.Entities.Agent", "Agent") + .WithMany() + .HasForeignKey("AgentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Agent"); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.ApiKey", b => + { + b.HasOne("Heimdall.Domain.Entities.Site", "Site") + .WithMany() + .HasForeignKey("SiteId") + .OnDelete(DeleteBehavior.Restrict); + + b.Navigation("Site"); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.AuditLog", b => + { + b.HasOne("Heimdall.Domain.Entities.AppUser", "User") + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.SetNull); + + b.Navigation("User"); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.Camera", b => + { + b.HasOne("Heimdall.Domain.Entities.Agent", "Agent") + .WithMany("Cameras") + .HasForeignKey("AgentId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("Heimdall.Domain.Entities.Site", "Site") + .WithMany("Cameras") + .HasForeignKey("SiteId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("Agent"); + + b.Navigation("Site"); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.CameraAssignment", b => + { + b.HasOne("Heimdall.Domain.Entities.Agent", "Agent") + .WithMany() + .HasForeignKey("AgentId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("Heimdall.Domain.Entities.Camera", "Camera") + .WithMany() + .HasForeignKey("CameraId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("Agent"); + + b.Navigation("Camera"); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.CameraCredentials", b => + { + b.HasOne("Heimdall.Domain.Entities.Camera", "Camera") + .WithOne("Credentials") + .HasForeignKey("Heimdall.Domain.Entities.CameraCredentials", "CameraId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Camera"); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.CameraDetectionProfile", b => + { + b.HasOne("Heimdall.Domain.Entities.Camera", "Camera") + .WithOne("DetectionProfile") + .HasForeignKey("Heimdall.Domain.Entities.CameraDetectionProfile", "CameraId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Camera"); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.CameraDiscoveryResult", b => + { + b.HasOne("Heimdall.Domain.Entities.Site", "Site") + .WithMany() + .HasForeignKey("SiteId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Site"); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.CameraEvent", b => + { + b.HasOne("Heimdall.Domain.Entities.Camera", "Camera") + .WithMany() + .HasForeignKey("CameraId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Camera"); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.DiscoveredStreamUrl", b => + { + b.HasOne("Heimdall.Domain.Entities.CameraDiscoveryResult", "DiscoveryResult") + .WithMany("DiscoveredStreams") + .HasForeignKey("DiscoveryResultId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("DiscoveryResult"); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.KnownFace", b => + { + b.HasOne("Heimdall.Domain.Entities.Person", "Person") + .WithMany("Faces") + .HasForeignKey("PersonId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Person"); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.UserGroup", b => + { + b.HasOne("Heimdall.Domain.Entities.Group", "Group") + .WithMany("UserGroups") + .HasForeignKey("GroupId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Heimdall.Domain.Entities.AppUser", "User") + .WithMany("UserGroups") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Group"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.UserSite", b => + { + b.HasOne("Heimdall.Domain.Entities.Site", "Site") + .WithMany() + .HasForeignKey("SiteId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("Heimdall.Domain.Entities.AppUser", "User") + .WithMany("UserSites") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Site"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("Heimdall.Domain.Entities.AppRole", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("Heimdall.Domain.Entities.AppUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("Heimdall.Domain.Entities.AppUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("Heimdall.Domain.Entities.AppRole", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Heimdall.Domain.Entities.AppUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("Heimdall.Domain.Entities.AppUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.Agent", b => + { + b.Navigation("Cameras"); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.AppUser", b => + { + b.Navigation("UserGroups"); + + b.Navigation("UserSites"); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.Camera", b => + { + b.Navigation("Credentials"); + + b.Navigation("DetectionProfile"); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.CameraDiscoveryResult", b => + { + b.Navigation("DiscoveredStreams"); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.Group", b => + { + b.Navigation("UserGroups"); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.Person", b => + { + b.Navigation("Faces"); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.Site", b => + { + b.Navigation("Agents"); + + b.Navigation("Cameras"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/Heimdall.Infrastructure/Migrations/20260719152833_AddPersonsAndKnownFaces.cs b/src/Heimdall.Infrastructure/Migrations/20260719152833_AddPersonsAndKnownFaces.cs new file mode 100644 index 0000000..d09beba --- /dev/null +++ b/src/Heimdall.Infrastructure/Migrations/20260719152833_AddPersonsAndKnownFaces.cs @@ -0,0 +1,84 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Heimdall.Infrastructure.Migrations +{ + /// + public partial class AddPersonsAndKnownFaces : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "persons", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + DisplayName = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), + Notes = table.Column(type: "character varying(2000)", maxLength: 2000, nullable: true), + PhotoUrl = table.Column(type: "character varying(2000)", unicode: false, maxLength: 2000, nullable: true), + Category = table.Column(type: "character varying(50)", unicode: false, maxLength: 50, nullable: true), + IsWatchlisted = table.Column(type: "boolean", nullable: false), + CreatedAt = table.Column(type: "timestamp with time zone", nullable: false), + UpdatedAt = table.Column(type: "timestamp with time zone", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_persons", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "known_faces", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + PersonId = table.Column(type: "uuid", nullable: false), + ThumbnailUrl = table.Column(type: "character varying(2000)", unicode: false, maxLength: 2000, nullable: true), + Embedding = table.Column(type: "jsonb", nullable: false), + SourceEventId = table.Column(type: "character varying(100)", unicode: false, maxLength: 100, nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_known_faces", x => x.Id); + table.ForeignKey( + name: "FK_known_faces_persons_PersonId", + column: x => x.PersonId, + principalTable: "persons", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_known_faces_PersonId", + table: "known_faces", + column: "PersonId"); + + migrationBuilder.CreateIndex( + name: "IX_persons_Category", + table: "persons", + column: "Category"); + + migrationBuilder.CreateIndex( + name: "IX_persons_DisplayName", + table: "persons", + column: "DisplayName"); + + migrationBuilder.CreateIndex( + name: "IX_persons_IsWatchlisted", + table: "persons", + column: "IsWatchlisted"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "known_faces"); + + migrationBuilder.DropTable( + name: "persons"); + } + } +} diff --git a/src/Heimdall.Infrastructure/Migrations/AppDbContextModelSnapshot.cs b/src/Heimdall.Infrastructure/Migrations/AppDbContextModelSnapshot.cs index b4a6631..b981d02 100644 --- a/src/Heimdall.Infrastructure/Migrations/AppDbContextModelSnapshot.cs +++ b/src/Heimdall.Infrastructure/Migrations/AppDbContextModelSnapshot.cs @@ -997,6 +997,81 @@ namespace Heimdall.Infrastructure.Migrations b.ToTable("groups", (string)null); }); + modelBuilder.Entity("Heimdall.Domain.Entities.KnownFace", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Embedding") + .IsRequired() + .HasColumnType("jsonb"); + + b.Property("PersonId") + .HasColumnType("uuid"); + + b.Property("SourceEventId") + .HasMaxLength(100) + .IsUnicode(false) + .HasColumnType("character varying(100)"); + + b.Property("ThumbnailUrl") + .HasMaxLength(2000) + .IsUnicode(false) + .HasColumnType("character varying(2000)"); + + b.HasKey("Id"); + + b.HasIndex("PersonId"); + + b.ToTable("known_faces", (string)null); + }); + + modelBuilder.Entity("Heimdall.Domain.Entities.Person", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Category") + .HasMaxLength(50) + .IsUnicode(false) + .HasColumnType("character varying(50)"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("DisplayName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("character varying(200)"); + + b.Property("IsWatchlisted") + .HasColumnType("boolean"); + + b.Property("Notes") + .HasMaxLength(2000) + .HasColumnType("character varying(2000)"); + + b.Property("PhotoUrl") + .HasMaxLength(2000) + .IsUnicode(false) + .HasColumnType("character varying(2000)"); + + b.Property("UpdatedAt") + .HasColumnType("timestamp with time zone"); + + b.HasKey("Id"); + + b.HasIndex("Category"); + + b.HasIndex("DisplayName"); + + b.HasIndex("IsWatchlisted"); + + b.ToTable("persons", (string)null); + }); + modelBuilder.Entity("Heimdall.Domain.Entities.Site", b => { b.Property("Id") @@ -1324,6 +1399,17 @@ namespace Heimdall.Infrastructure.Migrations b.Navigation("DiscoveryResult"); }); + modelBuilder.Entity("Heimdall.Domain.Entities.KnownFace", b => + { + b.HasOne("Heimdall.Domain.Entities.Person", "Person") + .WithMany("Faces") + .HasForeignKey("PersonId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Person"); + }); + modelBuilder.Entity("Heimdall.Domain.Entities.UserGroup", b => { b.HasOne("Heimdall.Domain.Entities.Group", "Group") @@ -1442,6 +1528,11 @@ namespace Heimdall.Infrastructure.Migrations b.Navigation("UserGroups"); }); + modelBuilder.Entity("Heimdall.Domain.Entities.Person", b => + { + b.Navigation("Faces"); + }); + modelBuilder.Entity("Heimdall.Domain.Entities.Site", b => { b.Navigation("Agents"); diff --git a/src/Heimdall.Infrastructure/Services/PersonService.cs b/src/Heimdall.Infrastructure/Services/PersonService.cs new file mode 100644 index 0000000..b995106 --- /dev/null +++ b/src/Heimdall.Infrastructure/Services/PersonService.cs @@ -0,0 +1,119 @@ +using Heimdall.Application.Common; +using Heimdall.Application.DTOs; +using Heimdall.Application.Services; +using Heimdall.Domain.Entities; +using Heimdall.Infrastructure.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; + +namespace Heimdall.Infrastructure.Services; + +public sealed class PersonService : IPersonService +{ + private readonly IDbContextFactory _dbFactory; + private readonly ILogger _logger; + + public PersonService(IDbContextFactory dbFactory, ILogger logger) + { + _dbFactory = dbFactory; + _logger = logger; + } + + public async Task> CreateAsync(CreatePersonRequest request, CancellationToken ct) + { + var person = new Person(request.DisplayName, request.Category); + if (request.Notes is not null) person.Update(request.DisplayName, request.Notes, request.Category, request.IsWatchlisted); + + await using var db = await _dbFactory.CreateDbContextAsync(ct); + db.Persons.Add(person); + await db.SaveChangesAsync(ct); + + _logger.LogInformation("Person created: {PersonId} ({Name})", person.Id, person.DisplayName); + return ServiceResult.Success(Map(person)); + } + + public async Task> UpdateAsync(Guid id, UpdatePersonRequest request, CancellationToken ct) + { + await using var db = await _dbFactory.CreateDbContextAsync(ct); + var person = await db.Persons.FindAsync([id], ct); + if (person is null) return ServiceResult.NotFound("Person not found"); + + person.Update(request.DisplayName, request.Notes, request.Category, request.IsWatchlisted); + await db.SaveChangesAsync(ct); + + return ServiceResult.Success(Map(person)); + } + + public async Task> GetByIdAsync(Guid id, CancellationToken ct) + { + await using var db = await _dbFactory.CreateDbContextAsync(ct); + var person = await db.Persons.Include(p => p.Faces).FirstOrDefaultAsync(p => p.Id == id, ct); + return person is null + ? ServiceResult.NotFound("Person not found") + : ServiceResult.Success(Map(person)); + } + + public async Task>> GetAllAsync(string? category, CancellationToken ct) + { + await using var db = await _dbFactory.CreateDbContextAsync(ct); + var query = db.Persons.Include(p => p.Faces).AsNoTracking(); + if (!string.IsNullOrEmpty(category)) + query = query.Where(p => p.Category == category); + + var persons = await query.OrderBy(p => p.DisplayName).ToListAsync(ct); + return ServiceResult>.Success(persons.Select(Map).ToList()); + } + + public async Task DeleteAsync(Guid id, CancellationToken ct) + { + await using var db = await _dbFactory.CreateDbContextAsync(ct); + var person = await db.Persons.FindAsync([id], ct); + if (person is null) return ServiceResult.NotFound("Person not found"); + + db.Persons.Remove(person); + await db.SaveChangesAsync(ct); + return ServiceResult.Success(); + } + + public async Task> RegisterFaceAsync(RegisterFaceRequest request, CancellationToken ct) + { + await using var db = await _dbFactory.CreateDbContextAsync(ct); + var person = await db.Persons.Include(p => p.Faces).FirstOrDefaultAsync(p => p.Id == request.PersonId, ct); + if (person is null) return ServiceResult.NotFound("Person not found"); + + var face = new KnownFace(request.PersonId, request.Embedding, request.ThumbnailUrl, request.SourceEventId); + person.AddFace(face); + await db.SaveChangesAsync(ct); + + return ServiceResult.Success(MapFace(face)); + } + + public async Task>> GetFacesAsync(Guid personId, CancellationToken ct) + { + await using var db = await _dbFactory.CreateDbContextAsync(ct); + var faces = await db.KnownFaces + .AsNoTracking() + .Where(f => f.PersonId == personId) + .ToListAsync(ct); + + return ServiceResult>.Success(faces.Select(MapFace).ToList()); + } + + public async Task DeleteFaceAsync(Guid faceId, CancellationToken ct) + { + await using var db = await _dbFactory.CreateDbContextAsync(ct); + var face = await db.KnownFaces.FindAsync([faceId], ct); + if (face is null) return ServiceResult.NotFound("Face not found"); + + db.KnownFaces.Remove(face); + await db.SaveChangesAsync(ct); + return ServiceResult.Success(); + } + + private static PersonResponse Map(Person p) => new( + p.Id, p.DisplayName, p.Notes, p.PhotoUrl, + p.Category, p.IsWatchlisted, p.Faces.Count, p.CreatedAt); + + private static KnownFaceResponse MapFace(KnownFace f) => new( + f.Id, f.PersonId, f.ThumbnailUrl, f.SourceEventId); +} diff --git a/src/Heimdall.Web/Components/Pages/Login.razor b/src/Heimdall.Web/Components/Pages/Login.razor index 9565197..8cc6577 100644 --- a/src/Heimdall.Web/Components/Pages/Login.razor +++ b/src/Heimdall.Web/Components/Pages/Login.razor @@ -8,41 +8,64 @@ @inject IJSRuntime JsRuntime @using Microsoft.AspNetCore.WebUtilities - - - - - Heimdall - - - Plateforme de surveillance vidéo - +
+
+
+
+ +
+

Heimdall

+

Plateforme de surveillance vidéo

+
-
- - +
+ + + - @if (!string.IsNullOrEmpty(_errorMessage)) - { - @_errorMessage - } +
+ @if (!string.IsNullOrEmpty(_errorMessage)) + { + @_errorMessage + } +
- - @if (_loading) { } - Se connecter - - + + @if (_loading) + { + + } + @(_loading ? "Connexion en cours…" : "Se connecter") + + +
- - Pas encore de compte ? - Créer un compte - - - + +
+
@code { private string _email = ""; diff --git a/src/Heimdall.Web/Components/Pages/Register.razor b/src/Heimdall.Web/Components/Pages/Register.razor index 5fad836..5781123 100644 --- a/src/Heimdall.Web/Components/Pages/Register.razor +++ b/src/Heimdall.Web/Components/Pages/Register.razor @@ -7,40 +7,72 @@ @inject HeimdallAuthStateProvider AuthStateProvider @inject IJSRuntime JsRuntime - - - Créer un compte +
+
+
+
+ +
+

Créer un compte

+

Rejoignez la plateforme Heimdall

+
-
- - - +
+ + + + - @if (!string.IsNullOrEmpty(_errorMessage)) - { - @_errorMessage - } +
+ @if (!string.IsNullOrEmpty(_errorMessage)) + { + @_errorMessage + } +
- - @if (_loading) { } - S'inscrire - - + + @if (_loading) + { + + } + @(_loading ? "Création en cours…" : "S'inscrire") + + +
- - Déjà un compte ? - Se connecter - - - + +
+
@code { private string _email = ""; diff --git a/src/Heimdall.Web/wwwroot/app.css b/src/Heimdall.Web/wwwroot/app.css index 562694b..8f7efb1 100644 --- a/src/Heimdall.Web/wwwroot/app.css +++ b/src/Heimdall.Web/wwwroot/app.css @@ -103,11 +103,194 @@ code { color: #fff !important; } -/* ─── Dark mode ────────────────────────────────────────────────── */ +/* ─── Auth pages (Login / Register) ─────────────────────────────── */ + +.auth-page { + display: flex; + align-items: center; + justify-content: center; + min-height: 100vh; + padding: 1.5rem; + background: linear-gradient(135deg, #0f172a 0%, #1e293b 50%, #0f172a 100%); + position: relative; + overflow: hidden; +} + +/* Decorative background circles */ +.auth-page::before { + content: ''; + position: absolute; + top: -25%; + right: -15%; + width: 500px; + height: 500px; + border-radius: 50%; + background: radial-gradient(circle, rgba(99, 102, 241, 0.15) 0%, transparent 70%); + pointer-events: none; +} + +.auth-page::after { + content: ''; + position: absolute; + bottom: -20%; + left: -10%; + width: 400px; + height: 400px; + border-radius: 50%; + background: radial-gradient(circle, rgba(168, 85, 247, 0.1) 0%, transparent 70%); + pointer-events: none; +} + +.auth-card { + width: 100%; + max-width: 420px; + background: rgba(255, 255, 255, 0.98); + border-radius: 20px; + padding: 2.5rem 2rem; + box-shadow: + 0 25px 50px -12px rgba(0, 0, 0, 0.25), + 0 0 0 1px rgba(255, 255, 255, 0.05); + position: relative; + z-index: 1; + animation: auth-card-enter 0.4s ease-out; +} + +@keyframes auth-card-enter { + from { + opacity: 0; + transform: translateY(16px) scale(0.98); + } + to { + opacity: 1; + transform: translateY(0) scale(1); + } +} + +.auth-header { + text-align: center; + margin-bottom: 2rem; +} + +.auth-header .auth-icon { + display: inline-flex; + align-items: center; + justify-content: center; + width: 56px; + height: 56px; + border-radius: 16px; + background: linear-gradient(135deg, #6366f1, #8b5cf6); + color: #fff; + margin-bottom: 1rem; + box-shadow: 0 8px 24px rgba(99, 102, 241, 0.3); +} + +.auth-header h1 { + font-size: 1.5rem; + font-weight: 700; + color: #1e293b; + margin: 0 0 0.25rem; + letter-spacing: -0.02em; +} + +.auth-header p { + font-size: 0.875rem; + color: #64748b; + margin: 0; +} + +.auth-form { + display: flex; + flex-direction: column; + gap: 0.75rem; +} + +.auth-form .mud-input-outlined { + background: #f8fafc; +} + +.auth-form .mud-button-submit { + height: 48px; + font-size: 0.95rem; + font-weight: 600; + border-radius: 12px; + text-transform: none; + letter-spacing: 0.01em; + margin-top: 0.25rem; +} + +.auth-error { + margin-bottom: 0.5rem; +} + +.auth-footer { + text-align: center; + margin-top: 1.5rem; + padding-top: 1.25rem; + border-top: 1px solid #e2e8f0; +} + +.auth-footer p { + font-size: 0.875rem; + color: #64748b; + margin: 0; +} + +.auth-footer a { + color: #6366f1; + font-weight: 600; + text-decoration: none; + transition: color 0.2s; +} + +.auth-footer a:hover { + color: #4f46e5; + text-decoration: underline; +} + +/* ─── Dark mode for auth pages ──────────────────────────────────── */ + +@media (prefers-color-scheme: dark) { + .auth-card { + background: rgba(30, 41, 59, 0.98); + box-shadow: + 0 25px 50px -12px rgba(0, 0, 0, 0.5), + 0 0 0 1px rgba(255, 255, 255, 0.05); + } + + .auth-header h1 { + color: #f1f5f9; + } + + .auth-header p { + color: #94a3b8; + } + + .auth-form .mud-input-outlined { + background: #1e293b; + } + + .auth-footer { + border-top-color: #334155; + } + + .auth-footer p { + color: #94a3b8; + } + + .auth-footer a { + color: #818cf8; + } + + .auth-footer a:hover { + color: #a5b4fc; + } +} + +/* ─── Dark mode (general) ──────────────────────────────────────── */ @media (prefers-color-scheme: dark) { html, body { - background-color: #1e1e2e; + background-color: #0f172a; } #blazor-error-ui {