using Cresce.Datasources.Repositories;
using Cresce.Models;

namespace Cresce.Services.PatientAccounts.InvoiceRemovers
{
    internal class AppointmentInvoiceRemover : IInvoiceRemover
    {
        private readonly IAppointmentRepository _appointmentRepository;

        public AppointmentInvoiceRemover(IAppointmentRepository appointmentRepository)
        {
            _appointmentRepository = appointmentRepository;
        }

        public AppointmentInvoiceRemover()
            : this(Configuration.Get<IAppointmentRepository>()) { }

        public void Remove(Invoice invoice)
        {
            var appointments = _appointmentRepository.GetAppointmentsForInvoice(invoice.Id);

            foreach (var appointment in appointments)
            {
                appointment.InvoiceId = null;

                _appointmentRepository.Save(appointment);
            }
        }
    }
}