using Data.SeaHavenIndustries; using Microsoft.EntityFrameworkCore; using SeaHaven.DataServices.Interfaces; namespace SeaHaven.DataServices.Implementation { public class WorkOrderDataService : IWorkOrderDataService { private readonly ApplicationDbContext _context; public WorkOrderDataService(ApplicationDbContext context) { _context = context; } public async Task GetByIdAsync(int id) { return await _context.workOrders.FindAsync(id); } public async Task GetByIdWithDetailsAsync(int id) { return await _context.workOrders .Include(w => w.Locations) .Include(w => w.AssignToUser) .FirstOrDefaultAsync(w => w.Id == id); } public async Task GetByInternalNumberAsync(string internalNumber) { return await _context.workOrders .FirstOrDefaultAsync(w => w.InternalWONumber == internalNumber); } public async Task> GetAllAsync() { return await _context.workOrders.ToListAsync(); } public async Task> GetByLocationIdAsync(int locationId) { return await _context.workOrders .Where(w => w.LocationId == locationId) .ToListAsync(); } public async Task> GetByAssigneeAsync(string assignee) { return await _context.workOrders .Where(w => w.AssignTo == assignee) .ToListAsync(); } public async Task> GetByStatusAsync(string status) { return await _context.workOrders .Where(w => w.Status == status) .ToListAsync(); } public async Task> GetByPriorityAsync(string priority) { return await _context.workOrders .Where(w => w.Priority == priority) .ToListAsync(); } public async Task<(IEnumerable Items, int TotalCount)> GetPagedAsync( int page, int pageSize, string? search = null, string? status = null, string? priority = null) { var query = _context.workOrders.AsQueryable(); if (!string.IsNullOrWhiteSpace(search)) { query = query.Where(w => w.WorkerOrderNumber!.Contains(search) || w.WorkerOrderTitle!.Contains(search)); } if (!string.IsNullOrWhiteSpace(status)) { query = query.Where(w => w.Status == status); } if (!string.IsNullOrWhiteSpace(priority)) { query = query.Where(w => w.Priority == priority); } var totalCount = await query.CountAsync(); var items = await query .OrderByDescending(w => w.CreatedDate) .Skip((page - 1) * pageSize) .Take(pageSize) .ToListAsync(); return (items, totalCount); } public async Task AddAsync(WorkOrder workOrder) { workOrder.CreatedDate = DateTime.Now; await _context.workOrders.AddAsync(workOrder); await _context.SaveChangesAsync(); return workOrder; } public async Task UpdateAsync(WorkOrder workOrder) { workOrder.LastModificationTime = DateTime.Now; _context.workOrders.Update(workOrder); await _context.SaveChangesAsync(); } public async Task DeleteAsync(int id) { var entity = await GetByIdAsync(id); if (entity != null) { _context.workOrders.Remove(entity); await _context.SaveChangesAsync(); } } public async Task> GetWorkOrderContactsAsync(int workOrderId) { return await _context.WorkOrderContacts .Where(wc => wc.WorkorderId == workOrderId) .ToListAsync(); } public async Task> GetWorkOrderCategoriesAsync(int workOrderId) { return await _context.workOrderCategories .Where(wc => wc.WorkorderId == workOrderId) .ToListAsync(); } public async Task> GetWorkOrderAttachmentsAsync(int workOrderId) { return await _context.workOrderAttachments .Where(wa => wa.WorkorderId == workOrderId) .ToListAsync(); } public async Task> GetWorkOrderCommentsAsync(int workOrderId) { return await _context.Comments .Where(c => c.WorkerOrderId == workOrderId) .ToListAsync(); } public async Task ExistsAsync(int id) { return await _context.workOrders.AnyAsync(w => w.Id == id); } public async Task InternalNumberExistsAsync(string internalNumber, int? excludeId = null) { return await _context.workOrders.AnyAsync(w => w.InternalWONumber == internalNumber && (excludeId == null || w.Id != excludeId)); } public async Task GenerateNextInternalWONumberAsync() { return await Task.FromResult($"WO-{DateTime.Now:yyyyMMdd}-{new Random().Next(1000, 9999)}"); } public async Task CountAsync() { return await _context.workOrders.CountAsync(); } public async Task CountByStatusAsync(string status) { return await _context.workOrders.CountAsync(w => w.Status == status); } public async Task CountByLocationAsync(int locationId) { return await _context.workOrders.CountAsync(w => w.LocationId == locationId); } } }