package models

import "time"

// BuildRequest represents a build job request from Appy
type BuildRequest struct {
	ID        string      `json:"id"`
	Platform  string      `json:"platform"` // "android-webview"
	Config    BuildConfig `json:"config"`
	BuildType string      `json:"build_type"` // "debug" or "release"
	CreatedAt time.Time   `json:"created_at"`

	// Callback context
	SiteURL      string `json:"-"` // Appy instance URL for callbacks
	PurchaseCode string `json:"-"` // Purchase code for authentication
}

// TriggerRequest is the request body for /trigger endpoint
type TriggerRequest struct {
	SiteURL string `json:"site_url" binding:"required"`
}

// BuildConfig represents the complete app configuration
type BuildConfig struct {
	// General Settings
	AppName     string `json:"app_name"`
	WebsiteURL  string `json:"website_url"`
	AppID       string `json:"app_id"`      // Package ID (e.g., com.example.myapp)
	Orientation string `json:"orientation"` // portrait, landscape, auto
	Language    string `json:"language"`    // en, es, fr, etc.
	UserAgent   string `json:"user_agent"`

	// WebView Settings
	EnableJavaScript bool `json:"enable_javascript"`  // Enable/disable JavaScript execution
	EnableDOMStorage bool `json:"enable_dom_storage"` // Enable/disable DOM storage (localStorage, sessionStorage)
	EnableZoom       bool `json:"enable_zoom"`        // Enable/disable zoom controls
	EnableCache      bool `json:"enable_cache"`       // Enable/disable HTTP caching

	// Design Settings
	ThemeColor          string `json:"theme_color"`           // Hex color
	TextColorLight      string `json:"text_color_light"`      // Hex color for light mode text
	TextColorDark       string `json:"text_color_dark"`       // Hex color for dark mode text
	EnableDynamicColors bool   `json:"enable_dynamic_colors"` // Enable Material You dynamic colors (Android 12+)
	ProgressIndicator   string `json:"progress_indicator"`    // disable, linear, circular
	ProgressColor       string `json:"progress_color"`        // Hex color
	ShowPageTitle       bool   `json:"show_page_title"`
	TabIconColorLight   string `json:"tab_icon_color_light"`   // Hex color for inactive tabs in light mode
	TabIconColorDark    string `json:"tab_icon_color_dark"`    // Hex color for inactive tabs in dark mode
	ActiveTabColorLight string `json:"active_tab_color_light"` // Hex color for active tab in light mode
	ActiveTabColorDark  string `json:"active_tab_color_dark"`  // Hex color for active tab in dark mode
	LayoutTemplate      string `json:"layout_template"`        // appbar-drawer, appbar-tabs, appbar, blank

	// Drawer Settings (only if layout = appbar-drawer)
	DrawerMode            string `json:"drawer_mode"`             // default, color, image
	DrawerBackgroundColor string `json:"drawer_background_color"` // Hex color
	DrawerBackgroundImage string `json:"drawer_background_image"` // URL or base64
	DrawerLogoEnabled     bool   `json:"drawer_logo_enabled"`
	DrawerLogo            string `json:"drawer_logo"`       // URL or base64 (deprecated - use DrawerLogoLight/Dark)
	DrawerLogoLight       string `json:"drawer_logo_light"` // URL or base64 for light mode
	DrawerLogoDark        string `json:"drawer_logo_dark"`  // URL or base64 for dark mode
	DrawerLogoSize        int    `json:"drawer_logo_size"`  // Logo size in dp (40-200)
	DrawerTitle           string `json:"drawer_title"`
	DrawerSubtitle        string `json:"drawer_subtitle"`
	DrawerTextColorMode   string `json:"drawer_text_color_mode"` // light, dark
	// Drawer item colors (matching bottom nav pattern)
	DrawerIconColorLight   string `json:"drawer_icon_color_light"`   // Hex color for inactive drawer icons in light mode
	DrawerIconColorDark    string `json:"drawer_icon_color_dark"`    // Hex color for inactive drawer icons in dark mode
	DrawerActiveColorLight string `json:"drawer_active_color_light"` // Hex color for active drawer item in light mode
	DrawerActiveColorDark  string `json:"drawer_active_color_dark"`  // Hex color for active drawer item in dark mode

	// Custom Code
	CustomCSS        string `json:"custom_css"`
	CustomJavaScript string `json:"custom_javascript"`

	// Icon Settings
	IconZipURL string `json:"icon_zip_url"` // URL to icon.kitchen ZIP file

	// Splashscreen Settings
	SplashBackgroundType string `json:"splash_background_type"` // color, image
	SplashBackground     string `json:"splash_background"`      // Hex color or image URL/base64
	SplashLogoEnabled    bool   `json:"splash_logo_enabled"`
	SplashLogo           string `json:"splash_logo"`      // URL or base64
	SplashLogoSize       int    `json:"splash_logo_size"` // Logo size in dp (60-240)
	SplashTitle          string `json:"splash_title"`
	SplashSubtitle       string `json:"splash_subtitle"`
	SplashTextColorMode  string `json:"splash_text_color_mode"` // light, dark
	SplashDelay          int    `json:"splash_delay"`           // Seconds

	// Navigation Settings
	BottomTabs       []NavigationItem `json:"bottom_tabs"`        // Only for appbar-tabs layout
	DrawerItems      []NavigationItem `json:"drawer_items"`       // Only for appbar-drawer layout
	AppBarButtons    []NavigationItem `json:"appbar_buttons"`     // Buttons in top app bar
	PullToRefresh    bool             `json:"pull_to_refresh"`    // Enable pull-to-refresh in WebView
	SwipeNavigation  bool             `json:"swipe_navigation"`   // Enable swipe back/forward navigation
	PreserveTabState bool             `json:"preserve_tab_state"` // Preserve tab/drawer navigation state

	// Splash Animation
	SplashAnimation string `json:"splash_animation"` // fade, slide, zoom, none

	// Permissions
	PermissionLocation     bool `json:"permission_location"`      // ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION
	PermissionCamera       bool `json:"permission_camera"`        // CAMERA
	PermissionStorage      bool `json:"permission_storage"`       // READ/WRITE_EXTERNAL_STORAGE, READ_MEDIA_*
	PermissionRecordAudio  bool `json:"permission_record_audio"`  // RECORD_AUDIO
	PermissionReadContacts bool `json:"permission_read_contacts"` // READ_CONTACTS
	PermissionVibrate      bool `json:"permission_vibrate"`       // VIBRATE

	// Build Configuration
	BuildFormat          string `json:"build_format"`          // apk, aab
	BuildConfigJSON      string `json:"build_config"`          // Custom build configuration JSON
	EnvironmentVariables string `json:"environment_variables"` // Environment variables JSON

	// Versioning
	AndroidVersionName string `json:"android_version_name"` // Android-specific version name
	VersionCode        int    `json:"version_code"`         // Android versionCode (build number)

	// Signing Settings (for release builds)
	KeystoreFile     string `json:"keystore_file"` // Path or URL to keystore
	KeystorePassword string `json:"keystore_password"`
	KeyAlias         string `json:"key_alias"`
	KeyPassword      string `json:"key_password"`

	// Push Notifications / Firebase
	FirebaseEnabled    bool   `json:"firebase_enabled"`     // Whether FCM is enabled
	GoogleServicesJSON string `json:"google_services_json"` // Content of google-services.json

	// WordPress-specific Settings (android-wordpress platform)
	SiteURL              string   `json:"site_url"`               // WordPress site URL
	SiteID               string   `json:"site_id"`                // WordPress.com site ID
	APINamespace         string   `json:"api_namespace"`          // WordPress API namespace (default: wp/v2)
	PostsPerPage         int      `json:"posts_per_page"`         // Posts per page (default: 10)
	ShowAuthor           bool     `json:"show_author"`            // Show author in post list
	ShowDate             bool     `json:"show_date"`              // Show date in post list
	ShowExcerpt          bool     `json:"show_excerpt"`           // Show excerpt in post list
	ShowFeaturedImage    bool     `json:"show_featured_image"`    // Show featured image in post list
	ShowCategories       bool     `json:"show_categories"`        // Show categories in post list
	ShowReadingTime      bool     `json:"show_reading_time"`      // Show reading time
	LayoutStyle          string   `json:"layout_style"`           // list, grid, magazine
	EnableComments       bool     `json:"enable_comments"`        // Enable comments
	EnableSearch         bool     `json:"enable_search"`          // Enable search
	EnableBookmarks      bool     `json:"enable_bookmarks"`       // Enable bookmarks
	EnableOfflineReading bool     `json:"enable_offline_reading"` // Enable offline reading (Room database)
	EnableShare          bool     `json:"enable_share"`           // Enable share functionality
	EnableUserAuth       bool     `json:"enable_user_auth"`       // Enable WordPress.com OAuth
	OAuthEnabled         bool     `json:"oauth_enabled"`          // OAuth is configured
	OAuthClientID        string   `json:"oauth_client_id"`        // WordPress.com OAuth client ID
	OAuthClientSecret    string   `json:"oauth_client_secret"`    // WordPress.com OAuth client secret
	OAuthRedirectScheme  string   `json:"oauth_redirect_scheme"`  // OAuth redirect scheme (default: appy-wp)
	OAuthScopes          string   `json:"oauth_scopes"`           // OAuth scopes
	HomeScreen           string   `json:"home_screen"`            // posts, categories, custom
	PostTypes            []string `json:"post_types"`             // Array of post types
	CategoriesFilter     []int    `json:"categories_filter"`      // Array of category IDs to filter
	TagsFilter           []int    `json:"tags_filter"`            // Array of tag IDs to filter
}

// NavigationItem represents a navigation tab or button
type NavigationItem struct {
	Label      string `json:"label"`
	Icon       string `json:"icon"`        // Icon name or resource
	ActionType string `json:"action_type"` // internal, external, share, email, call, category, tag, search, bookmarks, hosted_page
	URL        string `json:"url"`         // URL, email address, or phone number
	Value      string `json:"value"`       // Category ID, tag ID, search query, or page ID
	IsHome     bool   `json:"is_home"`     // Whether this is the home/default item
}

// BuildStatus represents the status of a build
type BuildStatus struct {
	BuildID      string    `json:"build_id"`
	Status       string    `json:"status"` // pending, processing, completed, failed
	Message      string    `json:"message"`
	ArtifactPath string    `json:"artifact_path"` // Path to generated APK/IPA (if completed)
	Error        string    `json:"error"`         // Error message (if failed)
	BuildLogs    string    `json:"build_logs"`    // Build logs
	StartedAt    time.Time `json:"started_at"`
	EndedAt      time.Time `json:"ended_at"`
}

// PendingBuildsResponse represents the response from Appy API
type PendingBuildsResponse struct {
	Builds []BuildRequest `json:"builds"`
}

// StatusUpdateRequest represents a status update to Appy
type StatusUpdateRequest struct {
	Status        string `json:"status"`
	Message       string `json:"message"`
	ArtifactPath  string `json:"artifact_path,omitempty"`
	Error         string `json:"error,omitempty"`
	BuildLogs     string `json:"build_logs,omitempty"`
	BuildDuration int    `json:"build_duration,omitempty"` // in seconds
}
