package migrate

import (
	"encoding/json"
	"time"
)

// ExportResult represents the complete export output.
type ExportResult struct {
	Version  string   `json:"version"`
	Source   Source   `json:"source"`
	Data     Data     `json:"data"`
	Warnings []string `json:"warnings,omitempty"`
}

// Source represents the source system information.
type Source struct {
	Type       string    `json:"type"`
	Version    string    `json:"version"`
	ExportedAt time.Time `json:"exported_at"`
}

// Data contains all exported entities.
type Data struct {
	Providers []Provider `json:"providers,omitempty"`
	Masters   []Master   `json:"masters,omitempty"`
	Keys      []Key      `json:"keys,omitempty"`
	Bindings  []Binding  `json:"bindings,omitempty"`
}

// Provider represents an EZ-API provider (mapped from New API channel).
type Provider struct {
	OriginalID   int      `json:"original_id"`
	Name         string   `json:"name"`
	Type         string   `json:"type"`
	BaseURL      string   `json:"base_url,omitempty"`
	APIKey       string   `json:"api_key"`
	Models       []string `json:"models,omitempty"`
	PrimaryGroup string   `json:"primary_group"`
	AllGroups    []string `json:"all_groups,omitempty"`
	Weight       int      `json:"weight"`
	Priority     int      `json:"priority,omitempty"`
	Status       string   `json:"status"`
	AutoBan      bool     `json:"auto_ban"`

	IsMultiKey    bool   `json:"is_multi_key,omitempty"`
	MultiKeyIndex int    `json:"multi_key_index,omitempty"`
	OriginalName  string `json:"original_name,omitempty"`

	Original json.RawMessage `json:"_original,omitempty"`
}

// Master represents an EZ-API master (inferred from New API user).
type Master struct {
	Name             string   `json:"name"`
	Group            string   `json:"group"`
	Namespaces       []string `json:"namespaces,omitempty"`
	DefaultNamespace string   `json:"default_namespace,omitempty"`
	MaxChildKeys     int      `json:"max_child_keys,omitempty"`
	GlobalQPS        int      `json:"global_qps,omitempty"`
	Status           string   `json:"status"`

	SourceUserID int    `json:"_source_user_id"`
	SourceEmail  string `json:"_source_email,omitempty"`
}

// Key represents an EZ-API key (mapped from New API token).
type Key struct {
	MasterRef     string `json:"master_ref"`
	OriginalToken string `json:"original_token"`
	Group         string `json:"group,omitempty"`
	Status        string `json:"status"`

	Scopes     []string `json:"scopes,omitempty"`
	Namespaces []string `json:"namespaces,omitempty"`

	ModelLimitsEnabled bool     `json:"model_limits_enabled,omitempty"`
	ModelLimits        []string `json:"model_limits,omitempty"`

	ExpiresAt *time.Time `json:"expires_at,omitempty"`

	AllowIPs []string `json:"allow_ips,omitempty"`

	QuotaLimit     *int64 `json:"quota_limit,omitempty"`
	QuotaUsed      *int64 `json:"quota_used,omitempty"`
	UnlimitedQuota bool   `json:"unlimited_quota,omitempty"`

	OriginalID              int  `json:"_original_id"`
	TokenPlaintextAvailable bool `json:"_token_plaintext_available,omitempty"`
}

// Binding represents an EZ-API binding (optional, from abilities).
type Binding struct {
	Namespace  string `json:"namespace"`
	RouteGroup string `json:"route_group"` // provider group name
	Model      string `json:"model"`
	Status     string `json:"status"`
}
