using Data.SeaHavenIndustries; using Microsoft.EntityFrameworkCore; using SeaHaven.Services.Interfaces; namespace SeaHaven.Services.Implementation { public class WorkOrderFieldLockService : IWorkOrderFieldLockService { private readonly ApplicationDbContext _context; public WorkOrderFieldLockService(ApplicationDbContext context) { _context = context; } public async Task IsLockedAsync(int workOrderId, string fieldName) { if (IsTracked(workOrderId, fieldName)) return true; return await _context.WorkOrderFieldLocks .AnyAsync(l => l.WorkOrderId == workOrderId && l.FieldName == fieldName); } public async Task LockFieldAsync(int workOrderId, string fieldName, string? userId) { if (IsTracked(workOrderId, fieldName)) return; if (await IsLockedAsync(workOrderId, fieldName)) return; _context.WorkOrderFieldLocks.Add(new WorkOrderFieldLock { WorkOrderId = workOrderId, FieldName = fieldName, LockedAt = DateTime.UtcNow, LockedByUserId = userId }); } private bool IsTracked(int workOrderId, string fieldName) => _context.WorkOrderFieldLocks.Local.Any(l => l.WorkOrderId == workOrderId && string.Equals(l.FieldName, fieldName, StringComparison.OrdinalIgnoreCase)); } }