package views

import (
	"fmt"

	tablomodel "xtablo-backend/internal/tablos"
	taskmodel "xtablo-backend/internal/tasks"
)

// TabloDetailTaskView holds a single task card in the tablo detail kanban.
type TabloDetailTaskView struct {
	ID         string
	Title      string
	DeleteHref string
	EditHref   string
}

// TabloDetailColumnView holds one kanban column in the tablo detail page.
type TabloDetailColumnView struct {
	ID         string
	Label      string
	Tasks      []TabloDetailTaskView
	CreateHref string
}

// TabloDetailEtapeView holds an etape summary in the tablo detail page.
type TabloDetailEtapeView struct {
	ID        string
	Name      string
	TaskCount int
}

// TabloDetailViewModel is the view model for the GET /tablos/{tabloID} page.
type TabloDetailViewModel struct {
	TabloID       string
	TabloName     string
	Color         string
	Initial       string
	OwnerName     string
	DueDate       string
	StatusLabel   string
	StatusTone    string
	Progress      int
	ProgressLabel string
	Columns       []TabloDetailColumnView
	Etapes        []TabloDetailEtapeView
}

// computeTabloProgress computes progress as (doneCount*100)/nonEtapeTotal.
// Etape tasks (IsEtape==true) are excluded from computation.
// Returns 0 if there are no non-etape tasks.
func computeTabloProgress(tasks []taskmodel.Record) int {
	total := 0
	done := 0
	for _, task := range tasks {
		if task.IsEtape {
			continue
		}
		total++
		if task.Status == taskmodel.StatusDone {
			done++
		}
	}
	if total == 0 {
		return 0
	}
	return (done * 100) / total
}

// NewTabloDetailViewModel builds a TabloDetailViewModel from a tablo record, its tasks, and the owner name.
func NewTabloDetailViewModel(tablo tablomodel.Record, tasks []taskmodel.Record, ownerName string) TabloDetailViewModel {
	statusLabel, _, _, statusTone := tabloStatusPresentation(tablomodel.Status(tablo.Status))
	progress := computeTabloProgress(tasks)

	columns := []TabloDetailColumnView{
		{ID: "todo", Label: "À faire"},
		{ID: "in_progress", Label: "En cours"},
		{ID: "in_review", Label: "En révision"},
		{ID: "done", Label: "Terminé"},
	}

	// Set CreateHref for each column
	for i := range columns {
		columns[i].CreateHref = "/tablos/" + tablo.ID.String() + "/tasks/create?status=" + columns[i].ID
	}

	// Populate columns with non-etape tasks
	for _, task := range tasks {
		if task.IsEtape {
			continue
		}
		taskView := TabloDetailTaskView{
			ID:         task.ID.String(),
			Title:      task.Title,
			DeleteHref: "/tasks/" + task.ID.String(),
			EditHref:   "/tasks/" + task.ID.String() + "/edit",
		}
		for i := range columns {
			if columns[i].ID == string(task.Status) {
				columns[i].Tasks = append(columns[i].Tasks, taskView)
				break
			}
		}
	}

	// Build etapes slice
	etapes := make([]TabloDetailEtapeView, 0)
	for _, task := range tasks {
		if !task.IsEtape {
			continue
		}
		count := 0
		for _, child := range tasks {
			if child.IsEtape {
				continue
			}
			if child.ParentTaskID != nil && *child.ParentTaskID == task.ID {
				count++
			}
		}
		etapes = append(etapes, TabloDetailEtapeView{
			ID:        task.ID.String(),
			Name:      task.Title,
			TaskCount: count,
		})
	}

	return TabloDetailViewModel{
		TabloID:       tablo.ID.String(),
		TabloName:     tablo.Name,
		Color:         tablo.Color,
		Initial:       projectInitialFromName(tablo.Name),
		OwnerName:     ownerName,
		DueDate:       "",
		StatusLabel:   statusLabel,
		StatusTone:    statusTone,
		Progress:      progress,
		ProgressLabel: fmt.Sprintf("%d%%", progress),
		Columns:       columns,
		Etapes:        etapes,
	}
}

// projectInitialFromName returns the uppercase first letter of the name.
// This mirrors the projectInitial function in the handlers package.
func projectInitialFromName(name string) string {
	trimmed := name
	if len(trimmed) == 0 {
		return "P"
	}
	runes := []rune(trimmed)
	result := string(runes[0])
	if len(result) == 0 {
		return "P"
	}
	// Convert to uppercase
	if result >= "a" && result <= "z" {
		result = string([]rune{runes[0] - 32})
	}
	return result
}

// tabloStatusPresentation mirrors the handler-side function to avoid import cycle.
// Returns (statusLabel, statusClass, progress, statusTone).
func tabloStatusPresentation(status tablomodel.Status) (string, string, int, string) {
	switch status {
	case tablomodel.StatusInProgress:
		return "En cours", "bg-[#FFF4E2] text-[#DB9729] border border-[#DB9729]", 50, "warning"
	case tablomodel.StatusDone:
		return "Terminé", "bg-green-50 text-green-600 border border-green-200", 100, "success"
	default:
		return "À faire", "bg-blue-50 text-blue-600 border border-blue-200", 0, "info"
	}
}

