package routes

import (
	"bssapp-backend/repository"
	"database/sql"
	"encoding/json"
	"net/http"
	"strconv"
	"time"
)

type ActivityLogQuery struct {
	Username       string
	RoleCode       string
	ActionCategory string
	ActionType     string
	ActionTarget   string
	Success        *bool

	DateFrom *time.Time
	DateTo   *time.Time

	Page  int
	Limit int
}

func AdminActivityLogsHandler(pgDB *sql.DB) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {

		q := repository.ActivityLogQuery{}

		// pagination
		if v := r.URL.Query().Get("page"); v != "" {
			if i, err := strconv.Atoi(v); err == nil {
				q.Page = i
			}
		}

		q.Limit = 1000
		if v := r.URL.Query().Get("limit"); v != "" {
			if i, err := strconv.Atoi(v); err == nil && i > 0 {
				q.Limit = i
			}
		}

		// filters
		q.Username = r.URL.Query().Get("username")
		q.RoleCode = r.URL.Query().Get("role_code")
		q.ActionCategory = r.URL.Query().Get("action_category")
		q.ActionType = r.URL.Query().Get("action_type")
		q.ActionTarget = r.URL.Query().Get("action_target")

		// success
		if v := r.URL.Query().Get("success"); v != "" {
			if v == "true" || v == "1" {
				b := true
				q.Success = &b
			} else if v == "false" || v == "0" {
				b := false
				q.Success = &b
			}
		}

		// status range
		if v := r.URL.Query().Get("status_min"); v != "" {
			if i, err := strconv.Atoi(v); err == nil {
				q.StatusMin = &i
			}
		}
		if v := r.URL.Query().Get("status_max"); v != "" {
			if i, err := strconv.Atoi(v); err == nil {
				q.StatusMax = &i
			}
		}

		// date range (ISO: 2026-01-03T00:00:00Z veya 2026-01-03)
		parseDate := func(s string) (*time.Time, error) {
			if s == "" {
				return nil, nil
			}
			// önce RFC3339 dene
			if t, err := time.Parse(time.RFC3339, s); err == nil {
				return &t, nil
			}
			// sonra sadece tarih
			if t, err := time.Parse("2006-01-02", s); err == nil {
				tt := t
				return &tt, nil
			}
			return nil, http.ErrNotSupported
		}

		if v := r.URL.Query().Get("date_from"); v != "" {
			t, err := parseDate(v)
			if err != nil {
				http.Error(w, "date_from format hatalı", http.StatusBadRequest)
				return
			}
			q.DateFrom = t
		}
		if v := r.URL.Query().Get("date_to"); v != "" {
			t, err := parseDate(v)
			if err != nil {
				http.Error(w, "date_to format hatalı", http.StatusBadRequest)
				return
			}
			q.DateTo = t
		}

		res, err := repository.ListActivityLogs(r.Context(), pgDB, q)
		if err != nil {
			http.Error(w, "Loglar alınamadı", http.StatusInternalServerError)
			return
		}

		w.Header().Set("Content-Type", "application/json")
		_ = json.NewEncoder(w).Encode(map[string]interface{}{
			"page":  q.Page,
			"limit": q.Limit,
			"total": res.Total,
			"items": res.Items,
		})
	}
}
