text: import { startOfHour, parseISO, isBefore, format } from 'date-fns';
  import pt from 'date-fns/locale/pt';
  
  import Appointment from '../models/Appointment';
  import User from '../models/User';
  import Notification from '../schemas/Notification';
  
  import Cache from '../../lib/Cache';
  
  class CreateAppointmentService {
    async run({ provider_id, user_id, date }) {
      if (provider_id === user_id) {
        throw new Error('You can not create appointments with yourself.');
      }
  
      const isProvider = await User.findOne({
        where: { id: provider_id, provider: true },
      });
  
      if (!isProvider) {
        throw new Error('You can only create appointments with providers.');
      }
  
      const hourStart = startOfHour(parseISO(date));
  
      if (isBefore(hourStart, new Date())) {
        throw new Error('Appointments for past dates are not allowed.');
      }
  
      const providerUnavailable = await Appointment.findOne({
        where: {
          provider_id,
          cancelled_at: null,
          date: hourStart,
        },
      });
  
      if (providerUnavailable) {
        throw new Error('Appointment date is not available.');
      }
  
      const appointment = await Appointment.create({
        user_id,
        provider_id,
        date: hourStart,
      });
  
      const user = await User.findByPk(user_id);
      const formattedDate = format(hourStart, "dd 'de' MMMM', as' H:mm'h'", {
        locale: pt,
      });
  
      await Notification.create({
        content: `Novo agendamento de ${user.name} para o dia ${formattedDate}`,
        user: provider_id,
      });
  
      Cache.invalidatePrefix(`user:${user_id}:appointments`);
  
      return appointment;
    }
  }
  
  export default new CreateAppointmentService();
  