﻿using Cresce.Datasources.Repositories;
using Cresce.Models;
using Cresce.Services.Exceptions;

namespace Cresce.Services.Contracts
{
    internal class ContractFactory : IContractFactory
    {
        private readonly IServicePackRepository _servicePackRepository;

        public ContractFactory(IServicePackRepository servicePackRepository)
        {
            _servicePackRepository = servicePackRepository;
        }
        
        public Contract Make(NewContract contract)
        {
            if (contract.ServicePackId == null)
            {
                throw new MissingServicePackException();
            }

            var servicePack = _servicePackRepository.GetById(contract.ServicePackId);

            return new Contract
            {
                PatientId = contract.PatientId,
                Start = contract.Start,
                End = contract.End,
                ServicePackId = servicePack.Id,
                ContractedServices = servicePack.Services,
                Contracted = servicePack.ContractedServices,
                Remaining = servicePack.ContractedServices
            };
        }
    }
}