using ConstructorApp.DataAccessLayer.Abstract;
using ConstructorApp.DataAccessLayer.Concrete;
using ConstructorApp.DataAccessLayer.Repositories;
using ConstructorApp.EntityLayer.Entities;
using Microsoft.EntityFrameworkCore;

namespace ConstructorApp.DataAccessLayer.EntityFramework
{
    public class EfProjectDal : GenericRepository<Project>, IProjectDal
    {
        private readonly ConstructorContext context;
        public EfProjectDal(ConstructorContext context) : base(context)
        {
            this.context = context;
        }

        public int CountAll()
        {
            return context.Projects.Count(x => x.IsActive == true);
        }

        public Project GetBySlug(string slug)
        {
            return context.Projects.FirstOrDefault(p => p.Slug == slug && p.IsActive);
        }

        public Project GetProjectWithImagesBySlug(string slug)
        {
            return context.Projects
            .Include(p => p.ProjectGallery)
            .FirstOrDefault(p => p.Slug == slug);
        }

        public void ProjectStatusActive(int id)
        {
            var values = context.Projects.Find(id);
            values.IsActive = true;
            context.SaveChanges();
        }

        public void ProjectStatusPassive(int id)
        {
            var values = context.Projects.Find(id);
            values.IsActive = false;
            context.SaveChanges();
        }
    }
}
