package todo

import (
	"connectrpc.com/connect"
	"connectrpc.com/validate"
	"context"
	"<@var(context.project.goprefix)>/<@var(context.project.name)>/db"
	todov1 "<@var(context.project.goprefix)>/<@var(context.project.name)>/gen/todo/v1"
	"<@var(context.project.goprefix)>/<@var(context.project.name)>/gen/todo/v1/todov1connect"
	. "<@var(context.project.goprefix)>/<@var(context.project.name)>/utils"
	"github.com/jackc/pgx/v5/pgtype"
	"net/http"
	"time"
)

type TodoServer struct{}

func (srv *TodoServer) CreateTodo(ctx context.Context, req *connect.Request[todov1.CreateTodoRequest]) (*connect.Response[todov1.CreateTodoResponse], error) {
	var id pgtype.UUID
	err := id.Scan(*req.Msg.Todo.Id)
	if err != nil {
		return nil, err
	}
	todo, err := db.Q.CreateTodo(ctx, db.CreateTodoParams{
		ID:   id,
		Task: req.Msg.Todo.Task,
	})
	if err != nil {
		return nil, err
	}
	return &connect.Response[todov1.CreateTodoResponse]{
		Msg: &todov1.CreateTodoResponse{
			Todo: &todov1.Todo{
				Id:        StrPtr(todo.ID.String()),
				Task:      todo.Task,
				CreatedAt: StrPtr(todo.CreatedAt.Time.Format(time.RFC3339)),
				UpdatesAt: StrPtr(todo.UpdatedAt.Time.Format(time.RFC3339)),
				Done:      BoolPtr(todo.Done.Bool),
			},
		},
	}, nil
}

func (srv *TodoServer) ListTodos(ctx context.Context, req *connect.Request[todov1.ListTodosRequest]) (*connect.Response[todov1.ListTodosResponse], error) {
	todos, err := db.Q.ListTodos(ctx)
	if err != nil {
		return nil, err
	}
	reponseTodos := []*todov1.Todo{}
	for _, todo := range todos {
		reponseTodos = append(reponseTodos, &todov1.Todo{
			Id:        StrPtr(todo.ID.String()),
			Task:      todo.Task,
			CreatedAt: StrPtr(todo.CreatedAt.Time.Format(time.RFC3339)),
			UpdatesAt: StrPtr(todo.UpdatedAt.Time.Format(time.RFC3339)),
			Done:      BoolPtr(todo.Done.Bool),
		})
	}

	return &connect.Response[todov1.ListTodosResponse]{
		Msg: &todov1.ListTodosResponse{
			Todos: reponseTodos,
		},
	}, nil
}

func (srv *TodoServer) UpdateTodo(ctx context.Context, req *connect.Request[todov1.UpdateTodoRequest]) (*connect.Response[todov1.UpdateTodoResponse], error) {
	var id pgtype.UUID
	err := id.Scan(*req.Msg.Todo.Id)
	if err != nil {
		return nil, err
	}
	todo, err := db.Q.UpdateTodo(ctx, db.UpdateTodoParams{
		Task: req.Msg.Todo.Task,
		Done: pgtype.Bool{
			Bool:  *req.Msg.Todo.Done,
			Valid: true,
		},
		ID: id,
	})
	if err != nil {
		return nil, err
	}
	return &connect.Response[todov1.UpdateTodoResponse]{
		Msg: &todov1.UpdateTodoResponse{
			Todo: &todov1.Todo{
				Id:        StrPtr(todo.ID.String()),
				Task:      todo.Task,
				CreatedAt: StrPtr(todo.CreatedAt.Time.Format(time.RFC3339)),
				UpdatesAt: StrPtr(todo.UpdatedAt.Time.Format(time.RFC3339)),
				Done:      BoolPtr(todo.Done.Bool),
			},
		},
	}, nil

}

func (srv *TodoServer) DeleteTodo(ctx context.Context, req *connect.Request[todov1.DeleteTodoRequest]) (*connect.Response[todov1.DeleteTodoResponse], error) {
	var id pgtype.UUID
	err := id.Scan(*req.Msg.Todo.Id)
	if err != nil {
		return nil, err
	}
	err = db.Q.DeleteTodo(ctx, id)
	if err != nil {
		return nil, err
	}
	return &connect.Response[todov1.DeleteTodoResponse]{
		Msg: &todov1.DeleteTodoResponse{},
	}, nil
}

func GetPathHandler() (path string, handler http.Handler) {
	path, handler = todov1connect.NewTodoServiceHandler(&TodoServer{}, connect.WithInterceptors(validate.NewInterceptor()))
	return
}
