package handlers

import (
	"context"
	"fmt"
	"github.com/goccy/go-json"
	"madsky.ru/go-finance/internal/auth"
	repository "madsky.ru/go-finance/internal/repository/user"
	"madsky.ru/go-finance/internal/server/response"
	"net/http"
)

func RegisterAuthRoutes(mux *http.ServeMux, ctx context.Context, repository repository.Repository) {
	mux.HandleFunc("POST /api/login", Login(ctx, repository))
}

func Login(ctx context.Context, repository repository.Repository) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		p := 0

		dec := json.NewDecoder(r.Body)
		dec.DisallowUnknownFields()

		var loginDto auth.LoginDto

		if err := dec.Decode(&loginDto); err != nil {
			response.Error(w, err, http.StatusBadRequest)
			return
		}

		fmt.Println(loginDto)

		response.WriteJSON(w, nil, http.StatusCreated, &p)
	}
}
