package bookings

import (
	"context"
	"time"

	"crussell/db"
)

// GetBookingStatus returns the status of a booking by ID.
// Uses db.Conn directly — not for use inside transactions.
func GetBookingStatus(ctx context.Context, bookingID string) (string, error) {
	var status string
	err := db.Conn.QueryRow(ctx, `SELECT status FROM bookings WHERE id = $1`, bookingID).Scan(&status)
	if err != nil {
		return "", err
	}
	return status, nil
}

// GetBookingStartTime returns the start_time of a booking by ID.
// Uses db.Conn directly — not for use inside transactions.
func GetBookingStartTime(ctx context.Context, bookingID string) (time.Time, error) {
	var startTime time.Time
	err := db.Conn.QueryRow(ctx, `SELECT start_time FROM bookings WHERE id = $1`, bookingID).Scan(&startTime)
	if err != nil {
		return time.Time{}, err
	}
	return startTime, nil
}

// BookingExists checks if a booking with the given ID exists.
// Uses db.Conn directly — not for use inside transactions.
func BookingExists(ctx context.Context, bookingID string) (bool, error) {
	var exists bool
	err := db.Conn.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM bookings WHERE id = $1)`, bookingID).Scan(&exists)
	return exists, err
}

// CountUserBookingsInStatus counts the number of bookings for a user with a specific status.
// Uses db.Conn directly — not for use inside transactions.
func CountUserBookingsInStatus(ctx context.Context, userID, status string) (int, error) {
	var count int
	err := db.Conn.QueryRow(ctx, `SELECT COUNT(*) FROM bookings WHERE user_id = $1 AND status = $2`, userID, status).Scan(&count)
	return count, err
}
