<?php
declare (strict_types = 1);

namespace app\admin\controller\api;

use think\admin\Controller;

/**
 * 邮箱配置接口
 */
class Mail extends Controller
{
    /**
     * 邮箱配置列表
     */
    private $mailConfigs = [
        [
            'name' => '默认配置1',
            'config' => [
                'DOMAIN' => '586vip.cn',
                'TEMP_MAIL' => 'ademyyk',
                'TEMP_MAIL_EXT' => '@mailto.plus',
                'BROWSER_USER_AGENT' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36',
                // 'BROWSER_PROXY' => 'http://127.0.0.1:2080',
                // 'BROWSER_HEADLESS' => 'True',
                'MAIL_SERVER' => 'https://tempmail.plus'
            ]
        ],
        [
            'name' => '备用配置1',
            'config' => [
                'DOMAIN' => 'nosqli.com',
                'TEMP_MAIL' => 'ademyyk',
                'TEMP_MAIL_EXT' => '@mailto.plus',
                'BROWSER_USER_AGENT' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36',
                'MAIL_SERVER' => 'https://tempmail.plus'
            ]
        ]
        // ,
        // [
        //     'name' => 'IMAP配置',
        //     'config' => [
        //         'DOMAIN' => 'wuen.site',
        //         'TEMP_MAIL' => null,
        //         'IMAP_SERVER' => 'imap.163.com',
        //         'IMAP_PORT' => 993,
        //         'IMAP_USER' => 'maticarmy@163.com',
        //         'IMAP_PASS' => 'LQer6rsSWan6vtuz',
        //         'BROWSER_USER_AGENT' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36',
        //         'MAIL_SERVER' => 'https://tempmail.plus'
        //     ]
        // ]
    ];

    /**
     * 获取所有配置
     */
    public function getAll()
    {
        return json([
            'code' => 0,
            'msg' => '获取成功',
            'data' => $this->mailConfigs
        ]);
    }

    /**
     * 获取随机配置
     */
    public function getRandom()
    {
        // 随机获取一个配置
        $config = $this->mailConfigs[array_rand($this->mailConfigs)];
        
        return json([
            'code' => 0,
            'msg' => '获取成功',
            'data' => [
                'name' => $config['name'],
                'env' => $config['config']
            ]
        ]);
    }

    /**
     * 获取指定配置
     */
    public function getConfig()
    {
        $name = input('name', '');
        
        // 如果未指定名称，返回随机配置
        if (empty($name)) {
            return $this->getRandom();
        }
        
        // 查找指定配置
        foreach ($this->mailConfigs as $config) {
            if ($config['name'] === $name) {
                return json([
                    'code' => 0,
                    'msg' => '获取成功',
                    'data' => [
                        'name' => $config['name'],
                        'env' => $config['config']
                    ]
                ]);
            }
        }
        
        // 未找到指定配置，返回随机配置
        return $this->getRandom();
    }
} 