package models

import (
	"time"
)

// APIResponse represents a standard API response
type APIResponse struct {
	Success bool        `json:"success"`
	Message string      `json:"message,omitempty"`
	Data    interface{} `json:"data,omitempty"`
	Error   string      `json:"error,omitempty"`
}

// LoginRequest represents a login request
type LoginRequest struct {
	Email    string `json:"email" binding:"required"`
	Password string `json:"password" binding:"required"`
}

// LoginResponse represents a login response
type LoginResponse struct {
	Token string `json:"token"`
	User  User   `json:"user"`
}

const (
	AppStatusBuilding  = "building"
	AppStatusDeploying = "deploying"
	AppStatusReady     = "ready"
	AppStatusFailed    = "failed"
)

type App struct {
	ID              uint      `json:"id" gorm:"primaryKey;autoIncrement"`
	UserID          uint      `json:"user_id" gorm:"not null;index"`
	Name            string    `json:"name" gorm:"not null" validate:"required"`
	Description     string    `json:"description"`
	Components      string    `json:"components" gorm:"type:text"`               // JSON array of Component IDs
	Status          string    `json:"status" gorm:"not null;default:'building'"` // e.g., AppStatusBuilding, AppStatusReady, AppStatusFailed
	PreviewID       uint      `json:"preview_id"`
	PreviewURL      string    `json:"preview_url"`
	CurrentImageTag string    `json:"current_image_tag"`
	CurrentImageURI string    `json:"current_image_uri"`
	ErrorMsg        string    `json:"error_msg"`
	CreatedAt       time.Time `json:"created_at" gorm:"autoCreateTime"`
	UpdatedAt       time.Time `json:"updated_at" gorm:"autoUpdateTime"`
}

// Component represents a deployable component
type Component struct {
	ID              uint      `json:"id" gorm:"primaryKey;autoIncrement"`
	UserID          uint      `json:"user_id" gorm:"not null;index"`
	Name            string    `json:"name" gorm:"not null"`
	Description     string    `json:"description"`
	Type            string    `json:"type" gorm:"not null"`   // web, api, database, etc.
	Status          string    `json:"status" gorm:"not null"` // active, inactive, deploying, etc.
	ErrorMsg        string    `json:"error_msg"`
	Config          string    `json:"config" gorm:"type:text"` // JSON configuration
	Repository      string    `json:"repository"`              // URL to the git repository
	Branch          string    `json:"branch"`
	SourceType      string    `json:"source_type,omitempty"`     // "autodetect", "dockerfile", "docker-compose"
	ServiceName     string    `json:"service_name,omitempty"`    // Service name from docker-compose (if applicable)
	BuildContext    string    `json:"build_context,omitempty"`   // Build context path for docker-compose services
	DockerfilePath  string    `json:"dockerfile_path,omitempty"` // Dockerfile path relative to build context
	CurrentImageTag string    `json:"current_image_tag"`
	CurrentImageURI string    `json:"current_image_uri"`
	CreatedAt       time.Time `json:"created_at" gorm:"autoCreateTime"`
	UpdatedAt       time.Time `json:"updated_at" gorm:"autoUpdateTime"`
}

// Deployment represents a deployment instance
type Deployment struct {
	ID          uint       `json:"id" gorm:"primaryKey;autoIncrement"`
	AppID       uint       `json:"app_id" gorm:"not null;index"`
	ClientID    uint       `json:"client_id" gorm:"not null;index"`
	Name        string     `json:"name" gorm:"not null"`
	Description string     `json:"description"`
	Environment string     `json:"environment" gorm:"default:'development'"` // dev, staging, prod
	Status      string     `json:"status" gorm:"default:'pending'"`          // pending, running, stopped, failed
	URL         string     `json:"url"`
	Config      string     `json:"config" gorm:"type:text;default:'{}'"` // JSON deployment configuration
	DeployedAt  *time.Time `json:"deployed_at,omitempty"`
	CreatedAt   time.Time  `json:"created_at" gorm:"autoCreateTime"`
	UpdatedAt   time.Time  `json:"updated_at" gorm:"autoUpdateTime"`
}

// Provider represents a cloud provider
type Provider struct {
	ID        uint      `json:"id" gorm:"primaryKey;autoIncrement"`
	Name      string    `json:"name" gorm:"not null"`
	Type      string    `json:"type" gorm:"not null"` // aws, digitalocean, ovh, etc.
	Config    string    `json:"config" gorm:"type:text;default:'{}'"`
	Active    bool      `json:"active" gorm:"default:true"`
	CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
	UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
}

// TicketStatus defines the possible statuses for a ticket.
const (
	TicketStatusOpen       = "open"
	TicketStatusInProgress = "in_progress"
	TicketStatusResolved   = "resolved"
	TicketStatusClosed     = "closed"
)

// TicketPriority defines the possible priorities for a ticket.
const (
	TicketPriorityLow      = "low"
	TicketPriorityMedium   = "medium"
	TicketPriorityHigh     = "high"
	TicketPriorityCritical = "critical"
)

// Ticket represents a support ticket
type Ticket struct {
	ID          uint       `json:"id" gorm:"primaryKey;autoIncrement"`
	ClientID    uint       `json:"client_id" gorm:"not null;index"` // Link to Client who reported it
	UserID      *uint      `json:"user_id,omitempty"`               // Link to User who reported it (optional)
	AssignedTo  *uint      `json:"assigned_to,omitempty"`           // Link to User it is assigned to (optional)
	Title       string     `json:"title" gorm:"not null"`
	Description string     `json:"description" gorm:"type:text"`
	Status      string     `json:"status" gorm:"default:'open'"`     // e.g., open, in_progress, resolved, closed
	Priority    string     `json:"priority" gorm:"default:'medium'"` // e.g., low, medium, high, critical
	CreatedAt   time.Time  `json:"created_at" gorm:"autoCreateTime"`
	UpdatedAt   time.Time  `json:"updated_at" gorm:"autoUpdateTime"`
	ResolvedAt  *time.Time `json:"resolved_at,omitempty"` // When the ticket was resolved
}

// TicketComment represents a comment on a support ticket
type TicketComment struct {
	ID        uint      `json:"id" gorm:"primaryKey;autoIncrement"`
	TicketID  uint      `json:"ticket_id" gorm:"not null;index"` // Link to the parent Ticket
	UserID    uint      `json:"user_id" gorm:"not null;index"`   // Link to User who made the comment
	Content   string    `json:"content" gorm:"type:text;not null"`
	CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
	UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
}

// Role constants for User
const (
	RoleAdmin  = "admin"
	RoleUser   = "user"
	RoleEditor = "editor" // Example additional role
)

// User represents a user in the system
type User struct {
	ID        uint      `json:"id" gorm:"primaryKey;autoIncrement"`
	Email     string    `json:"email" gorm:"uniqueIndex;not null" validate:"required,email"`
	Password  string    `json:"-" gorm:"not null"` // Never include password in JSON responses
	Name      string    `json:"name" gorm:"not null" validate:"required"`
	Role      string    `json:"role" gorm:"default:'user'" validate:"required,oneof=user admin editor"`
	Active    bool      `json:"active" gorm:"default:true"`
	CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
	UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
}

// Client represents a client in the system
type Client struct {
	ID          uint      `json:"id" gorm:"primaryKey;autoIncrement"`
	Name        string    `json:"name" gorm:"not null" validate:"required"`
	Description string    `json:"description"`
	ContactInfo string    `json:"contact_info"`
	Active      bool      `json:"active" gorm:"default:true"`
	CreatedAt   time.Time `json:"created_at" gorm:"autoCreateTime"`
	UpdatedAt   time.Time `json:"updated_at" gorm:"autoUpdateTime"`
}

// PreviewStatus defines the possible statuses for a preview.
const (
	PreviewStatusBuilding  = "building"
	PreviewStatusDeploying = "deploying"
	PreviewStatusRunning   = "running"
	PreviewStatusFailed    = "failed"
	PreviewStatusStopped   = "stopped"
)

type Preview struct {
	ID         uint      `json:"id" gorm:"primaryKey;autoIncrement"`
	AppID      uint      `json:"app_id" gorm:"not null;index"`
	Status     string    `json:"status" gorm:"not null;default:'building'"` // e.g., PreviewStatusBuilding, PreviewStatusRunning
	URL        string    `json:"url"`                                       // Preview URL (http://vps-ip)
	VPSID      string    `json:"vps_id"`                                    // OVH VPS ID
	IPAddress  string    `json:"ip_address"`                                // VPS IP address
	ErrorMsg   string    `json:"error_msg"`
	BuildLogs  string    `json:"build_logs" gorm:"type:text"`
	DeployLogs string    `json:"deploy_logs" gorm:"type:text"`
	ExpiresAt  time.Time `json:"expires_at" gorm:"not null"` // Auto-cleanup after X hours
	CreatedAt  time.Time `json:"created_at" gorm:"autoCreateTime"`
	UpdatedAt  time.Time `json:"updated_at" gorm:"autoUpdateTime"`
}
