package userManagement

import (
	"context"
	"fmt"
)

// ListDevicesParams 设备列表查询参数
type ListDevicesParams struct {
	Page     int    `json:"page"`
	PageSize int    `json:"page_size"`
	UserID   int64  `json:"user_id,omitempty"`
	IsActive *bool  `json:"is_active,omitempty"`
	IsTrusted *bool `json:"is_trusted,omitempty"`
}

// GetMyDevices 获取当前用户的设备列表
func (c *Client) GetMyDevices(ctx context.Context) ([]*Device, error) {
	resp, err := c.doRequest(ctx, "GET", "/api/v1/devices/me", nil)
	if err != nil {
		return nil, err
	}

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

	return result, nil
}

// GetTrustedDevices 获取信任设备列表
func (c *Client) GetTrustedDevices(ctx context.Context) ([]*Device, error) {
	resp, err := c.doRequest(ctx, "GET", "/api/v1/devices/me/trusted", nil)
	if err != nil {
		return nil, err
	}

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

	return result, nil
}

// GetDevice 获取设备详情
func (c *Client) GetDevice(ctx context.Context, id int64) (*Device, error) {
	resp, err := c.doRequest(ctx, "GET", fmt.Sprintf("/api/v1/devices/%d", id), nil)
	if err != nil {
		return nil, err
	}

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

	return &result, nil
}

// ListDevices 获取设备列表（管理员用）
func (c *Client) ListDevices(ctx context.Context, params *ListDevicesParams) (*PaginatedResponse, error) {
	if params.Page <= 0 {
		params.Page = 1
	}
	if params.PageSize <= 0 {
		params.PageSize = 20
	}

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

	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
}

// TrustDevice 信任设备
func (c *Client) TrustDevice(ctx context.Context, deviceID int64) error {
	resp, err := c.doRequest(ctx, "POST", fmt.Sprintf("/api/v1/devices/%d/trust", deviceID), nil)
	if err != nil {
		return err
	}

	return c.parseResponse(resp, nil)
}

// UntrustDevice 取消设备信任
func (c *Client) UntrustDevice(ctx context.Context, deviceID int64) error {
	resp, err := c.doRequest(ctx, "DELETE", fmt.Sprintf("/api/v1/devices/%d/trust", deviceID), nil)
	if err != nil {
		return err
	}

	return c.parseResponse(resp, nil)
}

// TrustDeviceByDeviceID 通过 device_id 信任设备
func (c *Client) TrustDeviceByDeviceID(ctx context.Context, deviceID string) error {
	resp, err := c.doRequest(ctx, "POST", fmt.Sprintf("/api/v1/devices/by-device-id/%s/trust", deviceID), nil)
	if err != nil {
		return err
	}

	return c.parseResponse(resp, nil)
}

// RevokeDevice 撤销设备
func (c *Client) RevokeDevice(ctx context.Context, deviceID int64) error {
	resp, err := c.doRequest(ctx, "DELETE", fmt.Sprintf("/api/v1/devices/%d", deviceID), nil)
	if err != nil {
		return err
	}

	return c.parseResponse(resp, nil)
}

// LogoutOtherDevices 登出其他设备
func (c *Client) LogoutOtherDevices(ctx context.Context, currentDeviceID string) error {
	req := map[string]string{"current_device_id": currentDeviceID}
	resp, err := c.doRequest(ctx, "POST", "/api/v1/devices/me/logout-others", req)
	if err != nil {
		return err
	}

	return c.parseResponse(resp, nil)
}
