using Microsoft.EntityFrameworkCore;
using WorkManagementTool.Data;
using WorkManagementTool.Data.Entities;

namespace WorkManagementTool.Services
{
    public class SchoolSubjectService
    {
        private readonly ApplicationDbContext _context;

        public SchoolSubjectService(ApplicationDbContext context)
        {
            _context = context;
        }

        public async Task<List<SchoolSubject>> GetAllSchoolSubjectsAsync()
        {
            return await _context.SchoolSubjects.ToListAsync();
        }

        public async Task<SchoolSubject?> GetSchoolSubjectByIdAsync(int id)
        {
            return await _context.SchoolSubjects
                .Include(i => i.Homeworks)
                .Where(w => w.Id == id)
                .FirstOrDefaultAsync();
        }

        public async Task AddSchoolSubjectAsync(SchoolSubject subject)
        {
            subject.Deleted = false;
            subject.CreatedAt = DateTime.UtcNow;

            _context.SchoolSubjects.Add(subject);
            await _context.SaveChangesAsync();
        }

        public async Task UpdateSchoolSubjectAsync(SchoolSubject subject)
        {
            _context.SchoolSubjects.Update(subject);
            await _context.SaveChangesAsync();
        }

        public async Task DeleteSchoolSubjectAsync(int id)
        {
            var subject = await _context.SchoolSubjects.FindAsync(id);
            if (subject != null)
            {
                _context.SchoolSubjects.Remove(subject);
                await _context.SaveChangesAsync();
            }
        }
    }
}
