package main

import (
	"fmt"
	"net/http"
	"os"
	"strings"
)

func (c *Crawler) StartDashboard(addr string) error {
	// Determine base URL for OAuth
	baseURL := os.Getenv("OAUTH_BASE_URL")
	if baseURL == "" {
		// Default based on whether we're in production
		if strings.Contains(addr, "0.0.0.0") {
			baseURL = "https://app.1440.news"
		} else {
			baseURL = "http://" + addr
		}
	}

	// Initialize OAuth manager
	oauthCfg, err := LoadOAuthConfig(baseURL)
	var oauth *OAuthManager
	if err != nil {
		fmt.Printf("OAuth not configured: %v (dashboard will be unprotected)\n", err)
	} else {
		oauth, err = NewOAuthManager(*oauthCfg, c.db)
		if err != nil {
			fmt.Printf("Failed to initialize OAuth: %v (dashboard will be unprotected)\n", err)
			oauth = nil
		} else {
			fmt.Println("OAuth authentication enabled for dashboard")
		}
	}

	// OAuth endpoints (always public)
	if oauth != nil {
		http.HandleFunc("/.well-known/oauth-client-metadata", oauth.HandleClientMetadata)
		http.HandleFunc("/.well-known/jwks.json", oauth.HandleJWKS)
		http.HandleFunc("/auth/login", oauth.HandleLogin)
		http.HandleFunc("/auth/callback", oauth.HandleCallback)
		http.HandleFunc("/auth/logout", oauth.HandleLogout)
		http.HandleFunc("/auth/session", oauth.HandleSessionInfo)
	}

	// Helper to wrap handlers with auth if OAuth is enabled
	withAuth := func(h http.HandlerFunc) http.HandlerFunc {
		if oauth != nil {
			return oauth.RequireAuth(h)
		}
		return h
	}

	http.HandleFunc("/dashboard", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleDashboard(w, r)
	}))

	// Root handler for url.1440.news short URLs and 1440.news accounts directory
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		host := r.Host
		// Strip port if present
		if idx := strings.Index(host, ":"); idx != -1 {
			host = host[:idx]
		}

		// If this is url.1440.news, treat path as short code
		if host == "url.1440.news" || host == "url.1440.localhost" {
			c.handleRedirect(w, r)
			return
		}

		// If this is 1440.news (apex), serve accounts directory
		if host == "1440.news" || host == "1440.localhost" {
			if r.URL.Path == "/" || r.URL.Path == "" {
				c.handleAccountsDirectory(w, r)
				return
			}
		}

		// Otherwise, redirect to dashboard for root path
		if r.URL.Path == "/" {
			http.Redirect(w, r, "/dashboard", http.StatusFound)
			return
		}

		// Unknown path
		http.NotFound(w, r)
	})

	http.HandleFunc("/api/stats", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIStats(w, r)
	}))
	http.HandleFunc("/api/allDomains", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIAllDomains(w, r)
	}))
	http.HandleFunc("/api/domainFeeds", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIDomainFeeds(w, r)
	}))
	http.HandleFunc("/api/feedInfo", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIFeedInfo(w, r)
	}))
	http.HandleFunc("/api/feedItems", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIFeedItems(w, r)
	}))
	http.HandleFunc("/api/search", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPISearch(w, r)
	}))
	http.HandleFunc("/api/tlds", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPITLDs(w, r)
	}))
	http.HandleFunc("/api/searchStats", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPISearchStats(w, r)
	}))
	http.HandleFunc("/api/tldDomains", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPITLDDomains(w, r)
	}))
	http.HandleFunc("/api/revisitDomain", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIRevisitDomain(w, r)
	}))
	http.HandleFunc("/api/priorityCrawl", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIPriorityCrawl(w, r)
	}))
	// Internal crawl endpoint (no auth) - not exposed via traefik
	http.HandleFunc("/internal/crawl", func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIPriorityCrawl(w, r)
	})
	http.HandleFunc("/api/checkFeed", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPICheckFeed(w, r)
	}))
	http.HandleFunc("/api/domainsByStatus", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIDomainsByStatus(w, r)
	}))
	http.HandleFunc("/api/feedsByStatus", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIFeedsByStatus(w, r)
	}))
	http.HandleFunc("/api/domains", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIDomains(w, r)
	}))
	http.HandleFunc("/api/feeds", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIFeeds(w, r)
	}))
	http.HandleFunc("/api/setDomainStatus", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPISetDomainStatus(w, r)
	}))
	http.HandleFunc("/api/filter", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIFilter(w, r)
	}))
	http.HandleFunc("/api/enablePublish", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIEnablePublish(w, r)
	}))
	http.HandleFunc("/api/disablePublish", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIDisablePublish(w, r)
	}))
	http.HandleFunc("/api/publishEnabled", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIPublishEnabled(w, r)
	}))
	http.HandleFunc("/api/publishDenied", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIPublishDenied(w, r)
	}))
	http.HandleFunc("/api/publishCandidates", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIPublishCandidates(w, r)
	}))
	http.HandleFunc("/api/setPublishStatus", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPISetPublishStatus(w, r)
	}))
	http.HandleFunc("/api/unpublishedItems", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIUnpublishedItems(w, r)
	}))
	http.HandleFunc("/api/testPublish", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPITestPublish(w, r)
	}))
	http.HandleFunc("/api/deriveHandle", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIDeriveHandle(w, r)
	}))
	http.HandleFunc("/api/publishFeed", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIPublishFeed(w, r)
	}))
	http.HandleFunc("/api/createAccount", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPICreateAccount(w, r)
	}))
	http.HandleFunc("/api/publishFeedFull", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIPublishFeedFull(w, r)
	}))
	http.HandleFunc("/api/updateProfile", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIUpdateProfile(w, r)
	}))
	http.HandleFunc("/api/languages", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPILanguages(w, r)
	}))
	http.HandleFunc("/api/denyDomain", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIDenyDomain(w, r)
	}))
	http.HandleFunc("/api/undenyDomain", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIUndenyDomain(w, r)
	}))
	http.HandleFunc("/api/dropDomain", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIDropDomain(w, r)
	}))
	http.HandleFunc("/api/tldStats", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPITLDStats(w, r)
	}))
	http.HandleFunc("/api/resetAllPublishing", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIResetAllPublishing(w, r)
	}))
	http.HandleFunc("/api/refreshProfiles", withAuth(func(w http.ResponseWriter, r *http.Request) {
		c.handleAPIRefreshProfiles(w, r)
	}))
	http.HandleFunc("/static/", func(w http.ResponseWriter, r *http.Request) {
		http.StripPrefix("/static/", http.FileServer(http.Dir("static"))).ServeHTTP(w, r)
	})

	fmt.Printf("Dashboard running at http://%s\n", addr)
	return http.ListenAndServe(addr, nil)
}
