package handlers

import (
	"encoding/json"
	"net/http"
	"strconv"

	"github.com/rede5/gohorsejobs/backend/internal/dto"
	"github.com/rede5/gohorsejobs/backend/internal/services"
)

type ApplicationHandler struct {
	Service *services.ApplicationService
}

func NewApplicationHandler(service *services.ApplicationService) *ApplicationHandler {
	return &ApplicationHandler{Service: service}
}

// CreateApplication submits a new job application
// @Summary      Create Application
// @Description  Submit a new job application for a posting
// @Tags         Applications
// @Accept       json
// @Produce      json
// @Param        application  body      dto.CreateApplicationRequest  true  "Application data"
// @Success      201          {object}  models.Application
// @Failure      400          {string}  string  "Bad Request"
// @Failure      500          {string}  string  "Internal Server Error"
// @Router       /applications [post]
func (h *ApplicationHandler) CreateApplication(w http.ResponseWriter, r *http.Request) {
	var req dto.CreateApplicationRequest
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	app, err := h.Service.CreateApplication(req)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusCreated)
	json.NewEncoder(w).Encode(app)
}

// GetApplications lists applications for a job
// @Summary      List Applications
// @Description  List all applications for a job
// @Tags         Applications
// @Accept       json
// @Produce      json
// @Param        jobId  query     int  true  "Filter applications by job ID"
// @Success      200    {array}   models.Application
// @Failure      400    {string}  string  "Bad Request"
// @Failure      500    {string}  string  "Internal Server Error"
// @Router       /applications [get]
func (h *ApplicationHandler) GetApplications(w http.ResponseWriter, r *http.Request) {
	// For now, simple get by Job ID query param
	jobIDStr := r.URL.Query().Get("jobId")
	if jobIDStr == "" {
		http.Error(w, "jobId is required", http.StatusBadRequest)
		return
	}
	jobID, err := strconv.Atoi(jobIDStr)
	if err != nil {
		http.Error(w, "Invalid jobId", http.StatusBadRequest)
		return
	}

	apps, err := h.Service.GetApplications(jobID)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(apps)
}

// GetApplicationByID returns a single application by ID
// @Summary      Get Application
// @Description  Retrieve a job application by its ID
// @Tags         Applications
// @Accept       json
// @Produce      json
// @Param        id   path      int  true  "Application ID"
// @Success      200  {object}  models.Application
// @Failure      400  {string}  string  "Bad Request"
// @Failure      404  {string}  string  "Not Found"
// @Router       /applications/{id} [get]
func (h *ApplicationHandler) GetApplicationByID(w http.ResponseWriter, r *http.Request) {
	idStr := r.PathValue("id")
	id, err := strconv.Atoi(idStr)
	if err != nil {
		http.Error(w, "Invalid application ID", http.StatusBadRequest)
		return
	}

	app, err := h.Service.GetApplicationByID(id)
	if err != nil {
		http.Error(w, err.Error(), http.StatusNotFound)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(app)
}

// UpdateApplicationStatus updates the status of an application
// @Summary      Update Application Status
// @Description  Update the status of a job application
// @Tags         Applications
// @Accept       json
// @Produce      json
// @Param        id    path      int                                true  "Application ID"
// @Param        body  body      dto.UpdateApplicationStatusRequest true  "Status update"
// @Success      200   {object}  models.Application
// @Failure      400   {string}  string  "Bad Request"
// @Failure      500   {string}  string  "Internal Server Error"
// @Router       /applications/{id}/status [put]
func (h *ApplicationHandler) UpdateApplicationStatus(w http.ResponseWriter, r *http.Request) {
	idStr := r.PathValue("id")
	id, err := strconv.Atoi(idStr)
	if err != nil {
		http.Error(w, "Invalid application ID", http.StatusBadRequest)
		return
	}

	var req dto.UpdateApplicationStatusRequest
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	app, err := h.Service.UpdateApplicationStatus(id, req)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(app)
}
