package models

import (
	"database/sql/driver"
	"encoding/json"
	"time"

	"github.com/google/uuid"
)

// InternetPackage represents an internet package/plan
type InternetPackage struct {
	ID       uuid.UUID  `json:"id" db:"id"`
	ClientID string     `json:"client_id" db:"client_id"` // character(36) in database

	// Basic Configuration
	PackageType     string  `json:"package_type" db:"package_type"`
	Name            string  `json:"name" db:"name"`
	Description     *string `json:"description" db:"description"`
	Duration        *string `json:"duration" db:"duration"`
	DurationMinutes *int64  `json:"duration_minutes" db:"duration_minutes"`

	// Speed & Performance
	UploadSpeed   *string `json:"upload_speed" db:"upload_speed"`
	DownloadSpeed *string `json:"download_speed" db:"download_speed"`
	Speed         *string `json:"speed" db:"speed"` // Legacy field

	// Pricing & Devices
	Amount     float64  `json:"amount" db:"amount"`
	Currency   string   `json:"currency" db:"currency"`
	Devices    *int64   `json:"devices" db:"devices"`
	TotalLimit *float64 `json:"total_limit" db:"total_limit"`

	// Burst Features
	BurstEnabled   bool    `json:"burst_enabled" db:"burst_enabled"`
	BurstLimit     *string `json:"burst_limit" db:"burst_limit"`
	BurstThreshold *string `json:"burst_threshold" db:"burst_threshold"`
	BurstTime      *int64  `json:"burst_time" db:"burst_time"`

	// Schedule Features
	ScheduleEnabled   bool     `json:"schedule_enabled" db:"schedule_enabled"`
	ScheduleStartTime *string  `json:"schedule_start_time" db:"schedule_start_time"`
	ScheduleEndTime   *string  `json:"schedule_end_time" db:"schedule_end_time"`
	AvailableDays     *JSONArray `json:"available_days" db:"available_days"`

	// Visibility & Metadata
	HiddenFromClient bool      `json:"hidden_from_client" db:"hidden_from_client"`
	Features         *JSONArray `json:"features" db:"features"`
	Category         *string   `json:"category" db:"category"`
	Popular          *bool     `json:"popular" db:"popular"`
	IsActive         bool      `json:"is_active" db:"is_active"`

	// Timestamps
	CreatedAt time.Time  `json:"created_at" db:"created_at"`
	UpdatedAt time.Time  `json:"updated_at" db:"updated_at"`
	DeletedAt *time.Time `json:"deleted_at" db:"deleted_at"`
}

// JSONArray represents a JSON array stored in the database
type JSONArray []string

// Value implements the driver.Valuer interface for database storage
func (j *JSONArray) Value() (driver.Value, error) {
	if j == nil || len(*j) == 0 {
		return nil, nil
	}
	return json.Marshal(*j)
}

// Scan implements the sql.Scanner interface for database retrieval
func (j *JSONArray) Scan(value interface{}) error {
	if value == nil {
		*j = JSONArray{}
		return nil
	}

	bytes, ok := value.([]byte)
	if !ok {
		return json.Unmarshal([]byte(value.(string)), j)
	}

	return json.Unmarshal(bytes, j)
}

// CreateInternetPackageRequest represents the request to create a new internet package
type CreateInternetPackageRequest struct {
	PackageType     string   `json:"package_type" validate:"required,oneof=Hotspot PPPoE Trial Bundles"`
	Name            string   `json:"name" validate:"required"`
	Description     *string  `json:"description"`
	Duration        *string  `json:"duration"`
	DurationMinutes *int64   `json:"duration_minutes"`
	UploadSpeed     *string  `json:"upload_speed"`
	DownloadSpeed   *string  `json:"download_speed"`
	Speed           *string  `json:"speed"`
	Amount          float64  `json:"amount" validate:"required,min=0"`
	Currency        *string  `json:"currency"`
	Devices         *int64   `json:"devices"`
	TotalLimit      *float64 `json:"total_limit"`
	BurstEnabled    *bool    `json:"burst_enabled"`
	BurstLimit      *string  `json:"burst_limit"`
	BurstThreshold  *string  `json:"burst_threshold"`
	BurstTime       *int64   `json:"burst_time"`
	ScheduleEnabled *bool    `json:"schedule_enabled"`
	ScheduleStartTime *string `json:"schedule_start_time"`
	ScheduleEndTime   *string `json:"schedule_end_time"`
	AvailableDays    *JSONArray `json:"available_days"`
	HiddenFromClient  *bool     `json:"hidden_from_client"`
	Features          *JSONArray `json:"features"`
	Category          *string    `json:"category"`
	Popular           *bool      `json:"popular"`
	IsActive          *bool      `json:"is_active"`
}

// UpdateInternetPackageRequest represents the request to update an internet package
type UpdateInternetPackageRequest struct {
	PackageType     *string   `json:"package_type" validate:"omitempty,oneof=Hotspot PPPoE Trial Bundles"`
	Name            *string  `json:"name"`
	Description     *string  `json:"description"`
	Duration        *string  `json:"duration"`
	DurationMinutes *int64   `json:"duration_minutes"`
	UploadSpeed     *string  `json:"upload_speed"`
	DownloadSpeed   *string  `json:"download_speed"`
	Speed           *string  `json:"speed"`
	Amount          *float64 `json:"amount" validate:"omitempty,min=0"`
	Currency        *string  `json:"currency"`
	Devices         *int64   `json:"devices"`
	TotalLimit      *float64 `json:"total_limit"`
	BurstEnabled    *bool    `json:"burst_enabled"`
	BurstLimit      *string  `json:"burst_limit"`
	BurstThreshold  *string  `json:"burst_threshold"`
	BurstTime       *int64   `json:"burst_time"`
	ScheduleEnabled *bool    `json:"schedule_enabled"`
	ScheduleStartTime *string `json:"schedule_start_time"`
	ScheduleEndTime   *string `json:"schedule_end_time"`
	AvailableDays    *JSONArray `json:"available_days"`
	HiddenFromClient  *bool     `json:"hidden_from_client"`
	Features          *JSONArray `json:"features"`
	Category          *string    `json:"category"`
	Popular           *bool      `json:"popular"`
	IsActive          *bool      `json:"is_active"`
}

// InternetPackageResponse represents the response for internet package operations
type InternetPackageResponse struct {
	ID                uuid.UUID  `json:"id"`
	ClientID          string     `json:"client_id"`
	PackageType       string     `json:"package_type"`
	Name              string     `json:"name"`
	Description       *string    `json:"description"`
	Duration          *string    `json:"duration"`
	DurationMinutes   *int64     `json:"duration_minutes"`
	UploadSpeed       *string    `json:"upload_speed"`
	DownloadSpeed     *string    `json:"download_speed"`
	Speed             *string    `json:"speed"`
	Amount            float64    `json:"amount"`
	Currency          string     `json:"currency"`
	Devices           *int64     `json:"devices"`
	TotalLimit        *float64   `json:"total_limit"`
	BurstEnabled      bool       `json:"burst_enabled"`
	BurstLimit        *string    `json:"burst_limit"`
	BurstThreshold    *string    `json:"burst_threshold"`
	BurstTime         *int64     `json:"burst_time"`
	ScheduleEnabled   bool       `json:"schedule_enabled"`
	ScheduleStartTime *string    `json:"schedule_start_time"`
	ScheduleEndTime   *string    `json:"schedule_end_time"`
	AvailableDays     *JSONArray `json:"available_days"`
	HiddenFromClient  bool       `json:"hidden_from_client"`
	Features          *JSONArray `json:"features"`
	Category          *string    `json:"category"`
	Popular           *bool      `json:"popular"`
	IsActive          bool       `json:"is_active"`
	CreatedAt         time.Time  `json:"created_at"`
	UpdatedAt         time.Time  `json:"updated_at"`
}

