<?php

namespace Controller\v2;

use Controller\Base;
use Lib\Imap\ImapSearch;
use Lib\Mail\Mail;
use Model\folderSql;
use Model\listsSql;
use Service\SyncMail;


/**
 * @author:dc
 * @time 2023/2/13 11:28
 * Class Home
 * @package Controller
 */
class Home extends Base {


    private function getFolderIds($email_id){
        $folder_ids   =   app()->request('folder_ids',[],['intval','abs']);

        if(is_array($folder_ids)){
            foreach ($folder_ids as $k=>$folder_id){
                if(!$folder_id){
                    unset($folder_ids[$k]);
                }
            }

            if($folder_ids){
                $folder_ids = array_values($folder_ids);
            }
        }

        // 默认查询 inbox
        if(!is_array($folder_ids) || !$folder_ids){
            $folder = app()->request('folder','收件箱');
            $folder_ids = db()->value(
                sprintf(
                    "select `id` from `%s` where `email_id` = %d and `folder` = '%s'",
                    folderSql::$table
                    ,$email_id,
                    addslashes($folder)
                )
            );

            $folder_ids = [$folder_ids];
        }

        if(!$folder_ids){
            return [-1];
        }
        return $folder_ids;
    }


    /**
     * 邮件列表 针对aicc应用那边
     * @author:dc
     * @time 2023/2/17 14:12
     */
    public function lists(){

        $limit   =   app()->request('limit',20,['intval','abs']);

        $last_id   =   app()->request('last_id',0,['intval','abs']);

        $udate = app()->request('udate',0,'intval');


        $range = [
            'uuid' => [
                'gte' => $last_id, // 大于等于开始日期
            ]
        ];
        if($udate){
            $range['udate'] = [
                'gte' => $udate, // 大于等于开始日期
            ];
        }

        $query = [
            'query' =>  [
                'bool'=>[
                    'must'=>[
                        ['term'=>[
                            'email_id'=>$this->getEmail('id')
                        ]],
                        ['terms'=>[
                            'folder_id'=>$this->getFolderIds($this->getEmail('id'))
                        ]],
                        'range'=>$range
                    ]
                ]
            ]
        ];

        $lists = es()->search($query,0,$limit,[],false);

        $lists = $lists['hits']['hits']??[];

        app()->_json(['data'=>$lists?:[]]);

    }


    /**
     * v2 版本
     * 同步规定 时间 之后的邮件
     * @return string
     * @throws \Lib\Err
     * @author:dc
     * @time 2023/8/2 16:19
     */
    public function sync(){

        ignore_user_abort(true);
        set_time_limit(0);

        $udate = app()->request('udate',0,'intval');
        if(!$udate){
            return 'no udate';
        }

        (new SyncMail($this->getEmail()))
            ->isUidAfter(2)
            ->search(
                (new ImapSearch())->dateGt($udate)
        )->sync();

        return 'ok';
    }

}