package handlers

import (
	"html/template"
	"net/http"
	"path/filepath"
	"strings"
)

type Handler struct{}

func New() *Handler { return &Handler{} }

type Service struct {
	Number, Icon, Title, Description string
	Tags                             []string
}
type Project struct {
	Number, Category, Title, Description, Class, Metric string
	Tags                                                []string
}
type PageData struct {
	Title, Description, Active, Eyebrow, Heading, Intro, Notice string
	Services                                                    []Service
	Projects                                                    []Project
}

var services = []Service{
	{"01", "⌘", "Custom software", "Purpose-built platforms that simplify complex operations, connect your data, and give your business room to grow.", []string{"Product engineering", "Workflow automation", "System integration"}},
	{"02", "↗", "Web development", "Fast, accessible websites and web applications designed to turn attention into meaningful action.", []string{"Business websites", "Web applications", "E-commerce"}},
	{"03", "▣", "Mobile applications", "Useful, polished iOS and Android experiences that keep your customers and teams connected.", []string{"iOS & Android", "React Native", "MVP development"}},
	{"04", "✳", "AI & automation", "Make repetitive work lighter with practical AI, intelligent workflows, and data-informed decisions.", []string{"AI integrations", "Process automation", "Data tools"}},
	{"05", "◈", "Product design & UX", "Clear journeys and considered interfaces that make digital products feel effortless to use.", []string{"UX research", "UI systems", "Prototyping"}},
	{"06", "☁", "Cloud & modernization", "Reliable foundations, thoughtful integrations, and scalable infrastructure for what's next.", []string{"Cloud readiness", "API development", "Maintenance"}},
}
var projects = []Project{
	{"01", "DIGITAL COMMERCE", "Commerce, without the friction", "A flexible commerce experience designed to make discovery, checkout, and repeat business feel effortless.", "project-orange", "+32%", []string{"Product design", "Web platform", "Integrations"}},
	{"02", "SAAS PLATFORM", "One clear view of the business", "A unified operations dashboard that brings important metrics and daily workflows into one place.", "project-blue", "4.2×", []string{"UX strategy", "Dashboard", "Data visualization"}},
	{"03", "MOBILE EXPERIENCE", "Better connections on the move", "A mobile-first service experience built around simple navigation, timely updates, and trust.", "project-lilac", "-41%", []string{"Mobile app", "Design system", "API"}},
}

func (h *Handler) render(w http.ResponseWriter, r *http.Request, page string, d PageData) {
	d.Active = page
	files := []string{
		filepath.Join("templates", "layouts", "base.html"),
		filepath.Join("templates", "partials", "navbar.html"),
		filepath.Join("templates", "partials", "footer.html"),
		filepath.Join("templates", "pages", page+".html"),
	}
	t, err := template.ParseFiles(files...)
	if err != nil {
		http.Error(w, "Template error: "+err.Error(), http.StatusInternalServerError)
		return
	}
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	if err := t.ExecuteTemplate(w, "base", d); err != nil {
		http.Error(w, "Unable to render page", http.StatusInternalServerError)
	}
}
func (h *Handler) Home(w http.ResponseWriter, r *http.Request) {
	h.render(w, r, "home", PageData{Title: "Digital products built for what's next", Description: "BINSOLS partners with ambitious teams to design, build, and grow digital products.", Eyebrow: "YOUR DIGITAL PARTNER", Heading: "We turn big ideas into\nuseful digital reality.", Intro: "BINSOLS is a software house for teams ready to move forward. We combine strategy, thoughtful design, and dependable engineering to build technology that works in the real world.", Services: services[:4], Projects: projects})
}
func (h *Handler) About(w http.ResponseWriter, r *http.Request) {
	h.render(w, r, "about", PageData{Title: "A thoughtful team for ambitious ideas", Description: "Get to know BINSOLS and our approach to building useful digital products.", Eyebrow: "WHO WE ARE", Heading: "Good technology starts with understanding.", Intro: "We bring product thinking, purposeful design, and reliable engineering together—so technology solves the right problem, not just the obvious one."})
}
func (h *Handler) Services(w http.ResponseWriter, r *http.Request) {
	h.render(w, r, "services", PageData{Title: "Software development services", Description: "Explore BINSOLS software, web, mobile, AI, UX, and cloud services.", Eyebrow: "WHAT WE DO", Heading: "From first sketch to dependable scale.", Intro: "A joined-up team for the full digital journey. Bring us a clear brief or a complicated challenge—we'll help you find the right next step.", Services: services})
}
func (h *Handler) Work(w http.ResponseWriter, r *http.Request) {
	h.render(w, r, "work", PageData{Title: "Selected work and capabilities", Description: "Explore illustrative BINSOLS digital product concepts and capabilities.", Eyebrow: "SELECTED WORK", Heading: "Ideas made tangible.", Intro: "A look at the kinds of digital experiences we help bring to life. These concept examples show our approach and are not presented as client case studies.", Projects: projects})
}
func (h *Handler) Insights(w http.ResponseWriter, r *http.Request) {
	h.render(w, r, "insights", PageData{Title: "Ideas for building better digital products", Description: "Practical perspectives from BINSOLS on software, product design, and digital growth.", Eyebrow: "NOTES & PERSPECTIVES", Heading: "A little clarity for your next big decision.", Intro: "Useful starting points for teams planning a new product, improving an existing platform, or making technology work harder."})
}
func (h *Handler) Contact(w http.ResponseWriter, r *http.Request) {
	h.render(w, r, "contact", PageData{Title: "Let's talk about your project", Description: "Tell BINSOLS what you're building and where you need help.", Eyebrow: "START A CONVERSATION", Heading: "Tell us what you're imagining.", Intro: "You don't need a perfect brief. Share what you're trying to achieve and we'll take it from there."})
}
func (h *Handler) SubmitContact(w http.ResponseWriter, r *http.Request) {
	if err := r.ParseForm(); err != nil {
		http.Error(w, "Please check the form and try again.", http.StatusBadRequest)
		return
	}
	name := strings.TrimSpace(r.FormValue("name"))
	email := strings.TrimSpace(r.FormValue("email"))
	message := strings.TrimSpace(r.FormValue("message"))
	if name == "" || email == "" || message == "" {
		h.render(w, r, "contact", PageData{Title: "Let's talk about your project", Description: "Tell BINSOLS about your project.", Eyebrow: "START A CONVERSATION", Heading: "Tell us what you're imagining.", Intro: "Please complete your name, email, and project details before sending.", Notice: "Please fill in your name, email, and project details."})
		return
	}
	// Demo mode: connect this handler to email or a database before production.
	h.render(w, r, "contact", PageData{Title: "Let's talk about your project", Description: "Tell BINSOLS about your project.", Eyebrow: "START A CONVERSATION", Heading: "Tell us what you're imagining.", Intro: "Thanks for reaching out. This demo form is working, but it does not yet deliver submissions to an inbox.", Notice: "Thanks, " + name + ". Your form passed validation. Connect the form handler to an email service or database to receive enquiries."})
}
func (h *Handler) NotFound(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusNotFound)
	h.render(w, r, "notfound", PageData{Title: "Page not found", Description: "The page you're looking for could not be found.", Eyebrow: "404 / NOT FOUND", Heading: "This page took a wrong turn.", Intro: "The page may have moved, or the link may be out of date."})
}
