package userManagement

import (
	"context"
	"fmt"
	"time"
)

// ListLoginLogsParams 登录日志查询参数
type ListLoginLogsParams struct {
	Page     int        `json:"page"`
	PageSize int        `json:"page_size"`
	UserID   int64      `json:"user_id,omitempty"`
	Status   int        `json:"status,omitempty"`
	StartAt  *time.Time `json:"start_at,omitempty"`
	EndAt    *time.Time `json:"end_at,omitempty"`
}

// ListOperationLogsParams 操作日志查询参数
type ListOperationLogsParams struct {
	Page      int        `json:"page"`
	PageSize  int        `json:"page_size"`
	UserID    int64      `json:"user_id,omitempty"`
	Action    string     `json:"action,omitempty"`
	Resource  string     `json:"resource,omitempty"`
	StartAt   *time.Time `json:"start_at,omitempty"`
	EndAt     *time.Time `json:"end_at,omitempty"`
}

// GetLoginLogs 获取登录日志列表
func (c *Client) GetLoginLogs(ctx context.Context, params *ListLoginLogsParams) (*PaginatedResponse, error) {
	if params.Page <= 0 {
		params.Page = 1
	}
	if params.PageSize <= 0 {
		params.PageSize = 20
	}

	path := fmt.Sprintf("/api/v1/logs/login?page=%d&page_size=%d", params.Page, params.PageSize)
	if params.UserID > 0 {
		path += fmt.Sprintf("&user_id=%d", params.UserID)
	}
	if params.Status > 0 {
		path += fmt.Sprintf("&status=%d", params.Status)
	}

	resp, err := c.doRequest(ctx, "GET", path, nil)
	if err != nil {
		return nil, err
	}

	var result PaginatedResponse
	if err := c.parseResponse(resp, &result); err != nil {
		return nil, err
	}

	return &result, nil
}

// GetOperationLogs 获取操作日志列表
func (c *Client) GetOperationLogs(ctx context.Context, params *ListOperationLogsParams) (*PaginatedResponse, error) {
	if params.Page <= 0 {
		params.Page = 1
	}
	if params.PageSize <= 0 {
		params.PageSize = 20
	}

	path := fmt.Sprintf("/api/v1/logs/operation?page=%d&page_size=%d", params.Page, params.PageSize)
	if params.UserID > 0 {
		path += fmt.Sprintf("&user_id=%d", params.UserID)
	}
	if params.Action != "" {
		path += "&action=" + params.Action
	}
	if params.Resource != "" {
		path += "&resource=" + params.Resource
	}

	resp, err := c.doRequest(ctx, "GET", path, nil)
	if err != nil {
		return nil, err
	}

	var result PaginatedResponse
	if err := c.parseResponse(resp, &result); err != nil {
		return nil, err
	}

	return &result, nil
}

// ExportLoginLogsRequest 导出登录日志请求
type ExportLoginLogsRequest struct {
	Format   string    `json:"format"` // "xlsx" or "csv"
	UserID   int64     `json:"user_id,omitempty"`
	Status   int       `json:"status,omitempty"`
	StartAt  *time.Time `json:"start_at,omitempty"`
	EndAt    *time.Time `json:"end_at,omitempty"`
	Fields   string    `json:"fields,omitempty"`
}

// ExportLoginLogs 导出登录日志（返回下载 URL）
func (c *Client) ExportLoginLogs(ctx context.Context, req *ExportLoginLogsRequest) (string, error) {
	resp, err := c.doRequest(ctx, "GET", "/api/v1/logs/login/export", req)
	if err != nil {
		return "", err
	}

	var result map[string]string
	if err := c.parseResponse(resp, &result); err != nil {
		return "", err
	}

	if url, ok := result["download_url"]; ok {
		return url, nil
	}

	return "", nil
}

// GetStats 获取统计信息
func (c *Client) GetStats(ctx context.Context) (*Stats, error) {
	resp, err := c.doRequest(ctx, "GET", "/api/v1/stats/dashboard", nil)
	if err != nil {
		return nil, err
	}

	var result Stats
	if err := c.parseResponse(resp, &result); err != nil {
		return nil, err
	}

	return &result, nil
}
