using Microsoft.EntityFrameworkCore;
using Models.Interfaces;
using Models.Models;

namespace Models.Repositories
{
    public class PhSFormSeriesRepository(PhronCareOperationsHubContext context) : IPhSFormSeriesRepository
    {
        #region Declaraciones
        private readonly PhronCareOperationsHubContext _context = context;
        #endregion

        #region Métodos

        public async Task<int> GetNextInternalNumberAsync(int formSeriesId)
        {
            var nextNumberParam = new Microsoft.Data.SqlClient.SqlParameter
            {
                ParameterName = "@NextNumber",
                SqlDbType = System.Data.SqlDbType.Int,
                Direction = System.Data.ParameterDirection.Output
            };

            await _context.Database.ExecuteSqlRawAsync(
                "EXEC dbo.PhS_FormSeries_GetNextNumber @FormSeriesId = {0}, @NextNumber = @NextNumber OUTPUT",
                formSeriesId,
                nextNumberParam
            );

            return (int)nextNumberParam.Value;
        }
        public async Task<PhSFormSeries?> GetByIdAsync(int formSeriesId)
        {
            return await _context.PhSFormSeries
                                 .AsNoTracking()
                                 .FirstOrDefaultAsync(s => s.Id == formSeriesId);
        }
        #endregion
    }
}
