package com.mvrent.carrental.service;

import com.mvrent.carrental.model.Booking;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * SMS notifications. Pluggable: today it logs the message (and no-ops when
 * disabled). Wire a provider (Twilio, MSG91, …) inside {@link #dispatch} to go live.
 */
@Service
@Slf4j
public class SmsService {

    @Value("${app.sms.enabled:false}")
    private boolean smsEnabled;

    public boolean isEnabled() { return smsEnabled; }

    @Async
    public void sendBookingConfirmation(Booking b) {
        if (b.getUser() == null) return;
        String phone = b.getUser().getPhone();
        if (phone == null || phone.isBlank()) return;
        dispatch(phone, "MV Rent: booking #" + b.getId() + " confirmed for "
                + b.getPickupDate() + ". Total " + b.getTotalAmount() + ".");
    }

    private void dispatch(String phone, String message) {
        if (!smsEnabled) {
            log.info("[sms skipped — disabled] to={} msg={}", phone, message);
            return;
        }
        // TODO: integrate an SMS gateway here (Twilio/MSG91/etc.).
        log.info("[sms] to={} msg={}", phone, message);
    }
}
