package tasks

import (
	"errors"
	"strings"
	"time"

	"github.com/google/uuid"
)

var ErrNotFound = errors.New("task not found")
var ErrInvalidParent = errors.New("invalid parent task")
var ErrInvalidAssignee = errors.New("invalid assignee")
var ErrInvalidStatus = errors.New("invalid task status")

type Status string

const (
	StatusTodo       Status = "todo"
	StatusInProgress Status = "in_progress"
	StatusInReview   Status = "in_review"
	StatusDone       Status = "done"
)

type Record struct {
	ID           uuid.UUID
	OwnerID      uuid.UUID
	TabloID      uuid.UUID
	Title        string
	Description  string
	Status       Status
	AssigneeID   *uuid.UUID
	IsEtape      bool
	ParentTaskID *uuid.UUID
	DueDate      *time.Time
	CreatedAt    time.Time
	UpdatedAt    time.Time
	DeletedAt    *time.Time
}

type CreateInput struct {
	OwnerID      uuid.UUID
	TabloID      uuid.UUID
	Title        string
	Description  string
	Status       Status
	AssigneeID   *uuid.UUID
	IsEtape      bool
	ParentTaskID *uuid.UUID
	DueDate      *time.Time
}

type UpdateInput struct {
	ID           uuid.UUID
	OwnerID      uuid.UUID
	TabloID      uuid.UUID
	Title        string
	Description  string
	Status       Status
	AssigneeID   *uuid.UUID
	ParentTaskID *uuid.UUID
	DueDate      *time.Time
}

type ListByTabloInput struct {
	OwnerID uuid.UUID
	TabloID uuid.UUID
}

type TaskView string

const (
	TaskViewKanban  TaskView = "kanban"
	TaskViewList    TaskView = "list"
	TaskViewRoadmap TaskView = "roadmap"
)

type TaskRoadmapMode string

const (
	TaskRoadmapModeWeek  TaskRoadmapMode = "week"
	TaskRoadmapModeMonth TaskRoadmapMode = "month"
)

type TaskPageState struct {
	View        TaskView
	RoadmapMode TaskRoadmapMode
	TabloIDs    []uuid.UUID
	AssigneeIDs []uuid.UUID
	Statuses    []Status
}

func ParseStatus(raw string) (Status, error) {
	switch Status(strings.TrimSpace(raw)) {
	case StatusTodo, StatusInProgress, StatusInReview, StatusDone:
		return Status(strings.TrimSpace(raw)), nil
	default:
		return "", ErrInvalidStatus
	}
}

func NormalizeTaskView(raw string) TaskView {
	switch TaskView(strings.TrimSpace(raw)) {
	case TaskViewList:
		return TaskViewList
	case TaskViewRoadmap:
		return TaskViewRoadmap
	default:
		return TaskViewKanban
	}
}

func NormalizeTaskRoadmapMode(raw string) TaskRoadmapMode {
	switch TaskRoadmapMode(strings.TrimSpace(raw)) {
	case TaskRoadmapModeMonth:
		return TaskRoadmapModeMonth
	default:
		return TaskRoadmapModeWeek
	}
}
