package services

import (
	"context"
	"crussell/auth"
	"crussell/db"
	"crussell/internal/validators"
	"crussell/mw"
	"database/sql"
	"encoding/json"
	"errors"
	"net/http"
	"strings"
	"time"

	"github.com/go-chi/chi/v5"
	"github.com/jackc/pgx/v5"
)

// Service represents a service in the system
type Service struct {
	ID                 string    `json:"id"`
	Name               string    `json:"name"`
	Description        string    `json:"description"`
	Price              float64   `json:"price"`
	DurationMinutes    int       `json:"duration_minutes"`
	IsActive           bool      `json:"is_active"`
	MinimumAgeRequired int       `json:"minimum_age_required"`
	CreatedAt          time.Time `json:"created_at"`
	CreatedBy          *string   `json:"created_by,omitempty"`
}

type ServiceResponse struct {
	ID                 string  `json:"id"`
	Name               string  `json:"name"`
	Description        string  `json:"description"`
	Price              float64 `json:"price"`
	DurationMinutes    int     `json:"duration_minutes"`
	MinimumAgeRequired int     `json:"minimum_age_required"`
	// Patch test status for non-admin users
	PatchTestStatus *string `json:"patch_test_status,omitempty"` // nil = not checked, "ok" = valid, "required" = no record, "expired" = record too old
}

// CreateServiceRequest represents the request payload for creating a new service
type CreateServiceRequest struct {
	Name               string  `json:"name" validate:"required,min=1,max=100"`
	Description        *string `json:"description,omitempty"`
	Price              float64 `json:"price" validate:"required,gt=0"`
	DurationMinutes    int     `json:"duration_minutes" validate:"required,gt=0"`
	MinimumAgeRequired int     `json:"minimum_age_required" validate:"gte=0,lte=100"`
}

// ToggleServiceHandler handles toggling a service's active status
func ToggleService(w http.ResponseWriter, r *http.Request) {
	serviceID := chi.URLParam(r, "id")
	if serviceID == "" || !validators.IsValidID(serviceID) {
		http.Error(w, "Service not found", http.StatusNotFound)
		return
	}

	query := "UPDATE services SET is_active = NOT is_active WHERE id = $1"
	result, err := db.DB.Exec(r.Context(), query, serviceID)
	if err != nil {
		http.Error(w, "Failed to toggle service: "+err.Error(), http.StatusInternalServerError)
		return
	}

	if result.RowsAffected() == 0 {
		http.Error(w, "Service not found", http.StatusNotFound)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)
	json.NewEncoder(w).Encode(map[string]interface{}{
		"message": "Service toggled successfully",
		"id":      serviceID,
	})
}

// POST /api/admin/services
func CreateServiceHandler(w http.ResponseWriter, r *http.Request) {
	// Parse and validate request
	var req CreateServiceRequest
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		http.Error(w, "Invalid JSON: "+err.Error(), http.StatusBadRequest)
		return
	}

	// Basic validation
	if req.Name == "" {
		http.Error(w, "Name is required", http.StatusBadRequest)
		return
	}
	if len(req.Name) > 100 {
		http.Error(w, "Name must be 100 characters or less", http.StatusBadRequest)
		return
	}
	if req.Price <= 0 {
		http.Error(w, "Price must be greater than 0", http.StatusBadRequest)
		return
	}
	if req.DurationMinutes <= 0 || req.DurationMinutes > 480 {
		http.Error(w, "Duration must be between 1 and 480 minutes", http.StatusBadRequest)
		return
	}
	if req.MinimumAgeRequired < 0 || req.MinimumAgeRequired > 100 {
		http.Error(w, "Minimum age must be between 0 and 100", http.StatusBadRequest)
		return
	}

	// Get user ID from context (if authentication is added later)
	var createdBy *string
	if userID, ok := r.Context().Value(mw.UserIDKey).(string); ok {
		createdBy = &userID
	}

	// Insert new service (no patch test columns)
	query := `
		INSERT INTO services (
			name, description, price, duration_minutes,
			minimum_age_required, created_by
		)
		VALUES ($1, $2, $3, $4, $5, $6)
		RETURNING
			id, name, description, price, duration_minutes, is_active,
			minimum_age_required, created_at, created_by
	`

	var service Service
	var createdByDB sql.NullString

	err := db.DB.QueryRow(r.Context(),
		query,
		req.Name,
		req.Description,
		req.Price,
		req.DurationMinutes,
		req.MinimumAgeRequired,
		createdBy,
	).Scan(
		&service.ID,
		&service.Name,
		&service.Description,
		&service.Price,
		&service.DurationMinutes,
		&service.IsActive,
		&service.MinimumAgeRequired,
		&service.CreatedAt,
		&createdByDB,
	)

	if err != nil {
		// Check for duplicate name or other constraints
		if err.Error() == "pq: duplicate key value violates unique constraint" {
			http.Error(w, "A service with this name already exists", http.StatusConflict)
			return
		}
		http.Error(w, "Failed to create service: "+err.Error(), http.StatusInternalServerError)
		return
	}

	// Convert nullable fields to pointers
	if createdByDB.Valid {
		service.CreatedBy = &createdByDB.String
	}

	// Return created service
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusCreated)
	if err := json.NewEncoder(w).Encode(service); err != nil {
		http.Error(w, "Failed to encode response: "+err.Error(), http.StatusInternalServerError)
		return
	}
}

// DeleteServiceHandler handles soft deleting a service (setting is_active to false)
func DeleteServiceHandler(w http.ResponseWriter, r *http.Request) {
	serviceID := chi.URLParam(r, "id")
	if serviceID == "" || !validators.IsValidID(serviceID) {
		http.Error(w, "Service not found", http.StatusNotFound)
		return
	}

	// Use soft delete - set is_active to FALSE instead of hard delete
	// This preserves referential integrity with booking_services
	query := "UPDATE services SET is_active = FALSE WHERE id = $1"
	result, err := db.DB.Exec(r.Context(), query, serviceID)
	if err != nil {
		http.Error(w, "Failed to delete service: "+err.Error(), http.StatusInternalServerError)
		return
	}

	if result.RowsAffected() == 0 {
		http.Error(w, "Service not found", http.StatusNotFound)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)
	json.NewEncoder(w).Encode(map[string]interface{}{
		"message": "Service deleted successfully",
		"id":      serviceID,
	})
}

// ServicesHandler returns all services from the database
// For non-admin logged-in users, filters based on age and patch test eligibility
func ServicesHandler(w http.ResponseWriter, r *http.Request) {
	// Check if user is authenticated - try context first, then optional token
	userID, hasUser := r.Context().Value(mw.UserIDKey).(string)
	role, _ := r.Context().Value(mw.UserRoleKey).(string)

	// If no user in context, try to parse token from header
	if !hasUser || userID == "" {
		authHeader := r.Header.Get("Authorization")
		if strings.HasPrefix(authHeader, "Bearer ") {
			tokenString := strings.TrimPrefix(authHeader, "Bearer ")
			var err error
			userID, role, err = auth.VerifyToken(tokenString, r.Context())
			if err != nil {
				// Invalid token - treat as unauthenticated
				userID = ""
				role = ""
			}
			hasUser = userID != ""
		}
	}

	// If not logged in or admin, return all services (current behavior)
	if !hasUser || userID == "" || role == "admin" {
		query := `
			SELECT id, name, description, price, duration_minutes,
			       minimum_age_required
			FROM services
			WHERE is_active = TRUE
			ORDER BY name
		`

		rows, err := db.DB.Query(r.Context(), query)
		if err != nil {
			http.Error(w, "Failed to fetch services: "+err.Error(), http.StatusInternalServerError)
			return
		}
		defer rows.Close()

		var services []ServiceResponse

		for rows.Next() {
			var service ServiceResponse

			err := rows.Scan(
				&service.ID,
				&service.Name,
				&service.Description,
				&service.Price,
				&service.DurationMinutes,
				&service.MinimumAgeRequired,
			)
			if err != nil {
				http.Error(w, "Failed to read service data: "+err.Error(), http.StatusInternalServerError)
				return
			}

			services = append(services, service)
		}

		if err = rows.Err(); err != nil {
			http.Error(w, "Error iterating over services: "+err.Error(), http.StatusInternalServerError)
			return
		}

		w.Header().Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusOK)

		if services == nil {
			services = []ServiceResponse{}
		}

		if err := json.NewEncoder(w).Encode(services); err != nil {
			http.Error(w, "Failed to encode response: "+err.Error(), http.StatusInternalServerError)
		}
		return
	}

	// User is logged in and not admin - check eligibility
	// Get user's date of birth
	var dob time.Time
	err := db.DB.QueryRow(r.Context(), `SELECT date_of_birth FROM users WHERE id = $1`, userID).Scan(&dob)
	if err != nil {
		http.Error(w, "Failed to get user data: "+err.Error(), http.StatusInternalServerError)
		return
	}

	// Calculate age
	now := time.Now()
	age := now.Year() - dob.Year()
	if now.YearDay() < dob.YearDay() {
		age--
	}

	// Get all active services
	query := `
		SELECT id, name, description, price, duration_minutes,
		       minimum_age_required
		FROM services
		WHERE is_active = TRUE
		ORDER BY name
	`

	rows, err := db.DB.Query(r.Context(), query)
	if err != nil {
		http.Error(w, "Failed to fetch services: "+err.Error(), http.StatusInternalServerError)
		return
	}
	defer rows.Close()

	var services []ServiceResponse
	var ineligibleServices []ServiceResponse

	for rows.Next() {
		var service ServiceResponse

		err := rows.Scan(
			&service.ID,
			&service.Name,
			&service.Description,
			&service.Price,
			&service.DurationMinutes,
			&service.MinimumAgeRequired,
		)
		if err != nil {
			http.Error(w, "Failed to read service data: "+err.Error(), http.StatusInternalServerError)
			return
		}

		// Check age eligibility - EXCLUDE if user is too young (can't be fixed by user)
		if age < service.MinimumAgeRequired {
			continue
		}

		// Check patch test requirement using new schema
		patchTestStatus := checkPatchTestStatus(r.Context(), userID, service.ID)
		if patchTestStatus != nil {
			// Patch test required - add status and put in ineligible list
			service.PatchTestStatus = patchTestStatus
			ineligibleServices = append(ineligibleServices, service)
			continue
		}

		// No patch test required or valid - include normally
		services = append(services, service)
	}

	if err = rows.Err(); err != nil {
		http.Error(w, "Error iterating over services: "+err.Error(), http.StatusInternalServerError)
		return
	}

	// Sort: eligible first (by name), then ineligible (by name)
	// Combine: eligible + ineligible
	services = append(services, ineligibleServices...)

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)

	if services == nil {
		services = []ServiceResponse{}
	}

	if err := json.NewEncoder(w).Encode(services); err != nil {
		http.Error(w, "Failed to encode response: "+err.Error(), http.StatusInternalServerError)
	}
}

// ServicesEligibleForUserHandler returns services with eligibility calculated for a specific user
// Used by admin booking flows when booking on behalf of a user
func ServicesEligibleForUserHandler(w http.ResponseWriter, r *http.Request) {
	userID := chi.URLParam(r, "user_id")
	if userID == "" || !validators.IsValidID(userID) {
		http.Error(w, "User not found", http.StatusNotFound)
		return
	}

	// Get user's date of birth
	var dob time.Time
	err := db.DB.QueryRow(r.Context(), `SELECT date_of_birth FROM users WHERE id = $1`, userID).Scan(&dob)
	if errors.Is(err, sql.ErrNoRows) {
		http.Error(w, "User not found", http.StatusNotFound)
		return
	}
	if err != nil {
		http.Error(w, "Failed to get user data: "+err.Error(), http.StatusInternalServerError)
		return
	}

	// Calculate age
	now := time.Now()
	age := now.Year() - dob.Year()
	if now.YearDay() < dob.YearDay() {
		age--
	}

	// Get all active services
	query := `
		SELECT id, name, description, price, duration_minutes,
		       minimum_age_required
		FROM services
		WHERE is_active = TRUE
		ORDER BY name
	`

	rows, err := db.DB.Query(r.Context(), query)
	if err != nil {
		http.Error(w, "Failed to fetch services: "+err.Error(), http.StatusInternalServerError)
		return
	}
	defer rows.Close()

	var services []ServiceResponse
	var grayedOutServices []ServiceResponse

	for rows.Next() {
		var service ServiceResponse

		err := rows.Scan(
			&service.ID,
			&service.Name,
			&service.Description,
			&service.Price,
			&service.DurationMinutes,
			&service.MinimumAgeRequired,
		)
		if err != nil {
			http.Error(w, "Failed to read service data: "+err.Error(), http.StatusInternalServerError)
			return
		}

		// Check age eligibility - EXCLUDE if user is too young
		if age < service.MinimumAgeRequired {
			continue
		}

		// Check patch test requirement using new schema
		patchTestStatus := checkPatchTestStatus(r.Context(), userID, service.ID)
		if patchTestStatus != nil {
			// Patch test required - add status and put in grayed out list
			service.PatchTestStatus = patchTestStatus
			grayedOutServices = append(grayedOutServices, service)
			continue
		}

		// No patch test required or valid - include normally
		services = append(services, service)
	}

	if err = rows.Err(); err != nil {
		http.Error(w, "Error iterating over services: "+err.Error(), http.StatusInternalServerError)
		return
	}

	// Sort and combine: valid first, then grayed out
	services = append(services, grayedOutServices...)

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)

	if services == nil {
		services = []ServiceResponse{}
	}

	if err := json.NewEncoder(w).Encode(services); err != nil {
		http.Error(w, "Failed to encode response: "+err.Error(), http.StatusInternalServerError)
	}
}

// checkPatchTestStatus checks if a service requires a patch test and if the user has a valid one
// Returns: nil = no patch test required, "required" = no record, "expired" = record too old
func checkPatchTestStatus(ctx context.Context, userID, serviceID string) *string {
	// Find patch tests that include this service
	var patchTestID string
	var noticeDurationHours int
	var expiryMonths int

	err := db.DB.QueryRow(ctx, `
		SELECT id, notice_duration_hours, expiry_months
		FROM patch_tests
		WHERE $1 = ANY(service_ids)
		LIMIT 1
	`, serviceID).Scan(&patchTestID, &noticeDurationHours, &expiryMonths)

	if errors.Is(err, sql.ErrNoRows) || errors.Is(err, pgx.ErrNoRows) {
		// No patch test required for this service
		return nil
	}
	if err != nil {
		// Database error - don't fail the whole request, just assume patch test required
		status := "required"
		return &status
	}

	// Check if user has a valid patch test record
	var testedAt time.Time
	err = db.DB.QueryRow(ctx, `
		SELECT tested_at
		FROM user_patch_tests
		WHERE user_id = $1 AND patch_test_id = $2
	`, userID, patchTestID).Scan(&testedAt)

	if errors.Is(err, sql.ErrNoRows) || errors.Is(err, pgx.ErrNoRows) {
		// No patch test record - required
		status := "required"
		return &status
	}
	if err != nil {
		// Database error
		status := "required"
		return &status
	}

	// Check if notice period has passed (can only book after this time)
	eligibleFrom := testedAt.Add(time.Duration(noticeDurationHours) * time.Hour)
	if time.Now().Before(eligibleFrom) {
		// Not yet eligible (within notice period)
		status := "required"
		return &status
	}

	// Check if patch test has expired
	expiresAt := testedAt.AddDate(0, expiryMonths, 0)
	if time.Now().After(expiresAt) {
		// Patch test expired
		status := "expired"
		return &status
	}

	// Patch test is valid
	status := "ok"
	return &status
}

// AllServicesHandler returns all services including inactive ones (useful for admin)
func AllServicesHandler(w http.ResponseWriter, r *http.Request) {
	// Query all services including inactive ones
	query := `
		SELECT id, name, description, price, duration_minutes, is_active,
		       minimum_age_required, created_at, created_by
		FROM services
		ORDER BY is_active DESC, name
	`

	rows, err := db.DB.Query(r.Context(), query)
	if err != nil {
		http.Error(w, "Failed to fetch services: "+err.Error(), http.StatusInternalServerError)
		return
	}
	defer rows.Close()

	var services []Service

	for rows.Next() {
		var service Service
		var createdBy sql.NullString

		err := rows.Scan(
			&service.ID,
			&service.Name,
			&service.Description,
			&service.Price,
			&service.DurationMinutes,
			&service.IsActive,
			&service.MinimumAgeRequired,
			&service.CreatedAt,
			&createdBy,
		)
		if err != nil {
			http.Error(w, "Failed to read service data: "+err.Error(), http.StatusInternalServerError)
			return
		}

		// Convert nullable fields to pointers
		if createdBy.Valid {
			service.CreatedBy = &createdBy.String
		}

		services = append(services, service)
	}

	if err = rows.Err(); err != nil {
		http.Error(w, "Error iterating over services: "+err.Error(), http.StatusInternalServerError)
		return
	}

	// Set response headers
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)

	// Return empty array instead of null if no services found
	if services == nil {
		services = []Service{}
	}

	// Encode response
	if err := json.NewEncoder(w).Encode(services); err != nil {
		http.Error(w, "Failed to encode response: "+err.Error(), http.StatusInternalServerError)
		return
	}
}
