package main

import (
	"html/template"
	"log"

	"github.com/pfeifferj/container-mom-go/pkg/billing"
	"github.com/pfeifferj/container-mom-go/pkg/config"
	"github.com/pfeifferj/container-mom-go/pkg/operator"
	"github.com/pfeifferj/container-mom-go/pkg/web/portal"
)

func main() {
	// Load config
	cfg, err := config.Load()
	if err != nil {
		log.Fatalf("Failed to load config: %v", err)
	}

	// Initialize billing service
	billingService := billing.NewService(cfg.StripeKey, cfg.StripeWebhookSecret)

	// Create operator
	op := operator.New(cfg, billingService)

	// Load templates
	tmpl, err := template.ParseGlob("/app/pkg/web/portal/templates/*.html")
	if err != nil {
		log.Fatalf("Failed to parse templates: %v", err)
	}

	// Initialize portal
	p := portal.NewPortal(
		op,
		tmpl,
		cfg.ListenAddr,
		cfg.StripeWebhookSecret,
	)

	// Start portal
	log.Printf("Starting server on %s", cfg.ListenAddr)
	if err := p.Start(); err != nil {
		log.Fatalf("Server error: %v", err)
	}
}
