using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.MAUI.DataLayer;
using WorkFlowCheck.MAUI.DataLayer.Entities;
using WorkFlowCheck.MAUI.Services.Interfaces;

namespace WorkFlowCheck.MAUI.Services
{
    public class BaseStockService : BaseService, IBaseStockService
    {
        public BaseStockService(HttpClient httpClient, IConfiguration configuration, AppDbContext dbContext, IMapper mapper) : base(httpClient, configuration, dbContext, mapper)
        {
        }

        public async Task<List<CheckPointDTO>> GetAllCheckPoints()
        {
            return _mapper.Map<List<CheckPointDTO>>(await _dbContext.CheckPoints.ToListAsync());

        }
        public async Task<CheckPointDTO> GetCheckPoint(int id)
        {
            return _mapper.Map<CheckPointDTO>(await _dbContext.CheckPoints.Where(w => w.Id == id).FirstOrDefaultAsync());
        }

        public async Task<CheckPointDTO> UpdateCheckPoint(CheckPointDTO checkPointDto)
        {
            // Lekérjük a meglévő entitást az adatbázisból
            try
            {
                var existingEntity = await _dbContext.CheckPoints.FindAsync(checkPointDto.Id);

                if (existingEntity == null)
                {
                    throw new Exception("CheckPoint not found.");
                }

                // Frissítjük az adatokat
                existingEntity.ShortName = checkPointDto.ShortName;
                existingEntity.Code = checkPointDto.Code;

                // Mivel a DbContext már követi az existingEntity-t, nem kell explicit Update() hívás
                await _dbContext.SaveChangesAsync();
            }
            catch (Exception ex)
            {


            }
            return checkPointDto;
        }

    }
}
