package interfaces

import (
	"context"

	"github.com/google/uuid"
	"github.com/RyanCopley/skybridge/user/internal/domain"
)

// UserRepository defines the interface for user data operations
type UserRepository interface {
	// Create creates a new user
	Create(ctx context.Context, user *domain.User) error
	
	// GetByID retrieves a user by ID
	GetByID(ctx context.Context, id uuid.UUID) (*domain.User, error)
	
	// GetByEmail retrieves a user by email
	GetByEmail(ctx context.Context, email string) (*domain.User, error)
	
	// Update updates an existing user
	Update(ctx context.Context, user *domain.User) error
	
	// Delete deletes a user by ID
	Delete(ctx context.Context, id uuid.UUID) error
	
	// List retrieves users with filtering and pagination
	List(ctx context.Context, req *domain.ListUsersRequest) (*domain.ListUsersResponse, error)
	
	// UpdateLastLogin updates the last login timestamp
	UpdateLastLogin(ctx context.Context, id uuid.UUID) error
	
	// Count returns the total number of users matching the filter
	Count(ctx context.Context, req *domain.ListUsersRequest) (int, error)
	
	// ExistsByEmail checks if a user exists with the given email
	ExistsByEmail(ctx context.Context, email string) (bool, error)
}

// UserProfileRepository defines the interface for user profile operations
type UserProfileRepository interface {
	// Create creates a new user profile
	Create(ctx context.Context, profile *domain.UserProfile) error
	
	// GetByUserID retrieves a user profile by user ID
	GetByUserID(ctx context.Context, userID uuid.UUID) (*domain.UserProfile, error)
	
	// Update updates an existing user profile
	Update(ctx context.Context, profile *domain.UserProfile) error
	
	// Delete deletes a user profile by user ID
	Delete(ctx context.Context, userID uuid.UUID) error
}

// UserSessionRepository defines the interface for user session operations
type UserSessionRepository interface {
	// Create creates a new user session
	Create(ctx context.Context, session *domain.UserSession) error
	
	// GetByToken retrieves a session by token
	GetByToken(ctx context.Context, token string) (*domain.UserSession, error)
	
	// GetByUserID retrieves all sessions for a user
	GetByUserID(ctx context.Context, userID uuid.UUID) ([]domain.UserSession, error)
	
	// Update updates an existing session (e.g., last used time)
	Update(ctx context.Context, session *domain.UserSession) error
	
	// Delete deletes a session by ID
	Delete(ctx context.Context, id uuid.UUID) error
	
	// DeleteByUserID deletes all sessions for a user
	DeleteByUserID(ctx context.Context, userID uuid.UUID) error
	
	// DeleteExpired deletes all expired sessions
	DeleteExpired(ctx context.Context) error
	
	// IsValidToken checks if a token is valid and not expired
	IsValidToken(ctx context.Context, token string) (bool, error)
}

// AuditRepository defines the interface for audit logging
type AuditRepository interface {
	// LogEvent logs an audit event
	LogEvent(ctx context.Context, event *AuditEvent) error
	
	// GetEvents retrieves audit events with filtering
	GetEvents(ctx context.Context, req *GetEventsRequest) (*GetEventsResponse, error)
}

// AuditEvent represents an audit event
type AuditEvent struct {
	ID          uuid.UUID `json:"id" db:"id"`
	Type        string    `json:"type" db:"type"`
	Severity    string    `json:"severity" db:"severity"`
	Status      string    `json:"status" db:"status"`
	Timestamp   string    `json:"timestamp" db:"timestamp"`
	ActorID     string    `json:"actor_id" db:"actor_id"`
	ActorType   string    `json:"actor_type" db:"actor_type"`
	ActorIP     string    `json:"actor_ip" db:"actor_ip"`
	UserAgent   string    `json:"user_agent" db:"user_agent"`
	ResourceID  string    `json:"resource_id" db:"resource_id"`
	ResourceType string   `json:"resource_type" db:"resource_type"`
	Action      string    `json:"action" db:"action"`
	Description string    `json:"description" db:"description"`
	Details     map[string]interface{} `json:"details" db:"details"`
	RequestID   string    `json:"request_id" db:"request_id"`
	SessionID   string    `json:"session_id" db:"session_id"`
}

// GetEventsRequest represents a request to get audit events
type GetEventsRequest struct {
	UserID       *uuid.UUID `json:"user_id,omitempty"`
	ResourceType *string    `json:"resource_type,omitempty"`
	Action       *string    `json:"action,omitempty"`
	StartTime    *string    `json:"start_time,omitempty"`
	EndTime      *string    `json:"end_time,omitempty"`
	Limit        int        `json:"limit,omitempty"`
	Offset       int        `json:"offset,omitempty"`
}

// GetEventsResponse represents a response for audit events
type GetEventsResponse struct {
	Events  []AuditEvent `json:"events"`
	Total   int          `json:"total"`
	Limit   int          `json:"limit"`
	Offset  int          `json:"offset"`
	HasMore bool         `json:"has_more"`
}