<?php
header('Content-Type: application/json');
require('../db.php');
require('../function/function.php');
blockedIP($_SERVER['REMOTE_ADDR']);

if (safetext(isset($_POST['page'])) && safetext(isset($_POST['userid'])) && safetext(isset($_POST['account_id'])) && safetext(isset($_COOKIE['loginkey'])) && safetext(isset($_POST['keyword']))) {
    $page = safetext($_POST['page']);
    $userId = safetext($_POST['userid']);
    $loginid = safetext($_POST['account_id']);
    $loginkey = safetext($_COOKIE['loginkey']);
    $keyword = safetext($_POST['keyword']);

    $is_login = uwuzuUserLoginCheck($loginid, $loginkey, "user");
    if ($is_login === false) {
        echo json_encode(['success' => false, 'error' => 'bad_request']);
        exit;
    }

    // データベースに接続
    try {
        $option = array(
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::MYSQL_ATTR_MULTI_STATEMENTS => false
        );
        $pdo = new PDO('mysql:charset=utf8mb4;dbname=' . DB_NAME . ';host=' . DB_HOST, DB_USER, DB_PASS, $option);
    } catch (PDOException $e) {
        // 接続エラーのときエラー内容を取得する
        $error_message[] = $e->getMessage();
    }

    if (!empty($pdo)) {
        $myUserData = getUserData($pdo, $userId);
        $myblocklist = safetext($myUserData["blocklist"]);
        $mybookmark = safetext($myUserData["bookmark"]);

        $itemsPerPage = 15; // 1ページあたりのユーズ数
        $pageNumber = $page;
        if($pageNumber <= 0 || (!(is_numeric($pageNumber)))){
            $pageNumber = 1;
        }
        $offset = ($pageNumber - 1) * $itemsPerPage;

        $messages = array();
        
        if (!empty($keyword)) {
            $aduserinfoQuery = $pdo->prepare("SELECT username,userid,loginid,admin,role,sacinfo,blocklist,bookmark FROM account WHERE userid = :userid");
            $aduserinfoQuery->bindValue(':userid', safetext($userId));
            $aduserinfoQuery->execute();
            $res = $aduserinfoQuery->fetch();
            $myblocklist = safetext($res["blocklist"]);
            $mybookmark = safetext($res["bookmark"]);

            $keywordPattern = '/from:@(\w+)\s+(.+)/';
            if (preg_match($keywordPattern, $keyword, $matches)) {
                $username = $matches[1];
                $searchKeyword = $matches[2];

                $messageQuery = $pdo->prepare("SELECT * FROM ueuse WHERE account = :username AND (ueuse LIKE :searchKeyword OR abi LIKE :searchKeyword) ORDER BY datetime DESC LIMIT :offset, :itemsPerPage");
                $messageQuery->bindValue(':username', $username, PDO::PARAM_STR);
                $messageQuery->bindValue(':searchKeyword', '%' . $searchKeyword . '%', PDO::PARAM_STR);
                $messageQuery->bindValue(':offset', $offset, PDO::PARAM_INT);
                $messageQuery->bindValue(':itemsPerPage', $itemsPerPage, PDO::PARAM_INT);
                $messageQuery->execute();
                $message_array = $messageQuery->fetchAll();
            } else {
                $messageQuery = $pdo->prepare("SELECT * FROM ueuse WHERE ueuse LIKE :keyword OR abi LIKE :keyword ORDER BY datetime DESC LIMIT :offset, :itemsPerPage");
                $messageQuery->bindValue(':keyword', '%' . $keyword . '%', PDO::PARAM_STR);
                $messageQuery->bindValue(':offset', $offset, PDO::PARAM_INT);
                $messageQuery->bindValue(':itemsPerPage', $itemsPerPage, PDO::PARAM_INT);
                $messageQuery->execute();
                $message_array = $messageQuery->fetchAll();
            }

            $user_keyword = str_replace('@', '', $keyword);
            $usersQuery = $pdo->prepare("SELECT * FROM account WHERE username LIKE :keyword OR userid LIKE :keyword OR profile LIKE :keyword ORDER BY datetime DESC LIMIT :offset, :itemsPerPage");
            $usersQuery->bindValue(':offset', $offset, PDO::PARAM_INT);
            $usersQuery->bindValue(':itemsPerPage', $itemsPerPage, PDO::PARAM_INT);
            $usersQuery->bindValue(':keyword', '%' . $user_keyword . '%', PDO::PARAM_STR);
            $usersQuery->execute();
            $users_array = $usersQuery->fetchAll();

            $users = array();
            foreach ($users_array as $row) {
                $users[] = $row;
            }
                
            $messages = array();
            foreach ($message_array as $row) {
                $messages[] = $row;
            }

            // ユーザー情報を取得して、$messages内のusernameをuserDataのusernameに置き換える
            $messages = getDatasUeuse($pdo, $messages);
            //adsystem------------------

            $message['ads'] = "false";

            $today = date("Y-m-d H:i:s");

            $adsQuery = $pdo->prepare("SELECT * FROM ads WHERE start_date < :today AND limit_date > :today ORDER BY rand()");
            $adsQuery->bindValue(':today', $today);
            $adsQuery->execute();
            $adsresult = $adsQuery->fetch();
            if(!(empty($adsresult))){
                $message['ads'] = "true";
                $message['ads_url'] = $adsresult["url"];
                $message['ads_img_url'] = $adsresult["image_url"];
                $message['ads_memo'] = $adsresult["memo"];
            }
            //--------------------------
            $ueuseItems = array();
            if(!empty($messages)){
                foreach ($messages as $value) {
                    $formatted = FormatUeuseItem($value, $myblocklist, $mybookmark, $pdo, $userId);
                    if ($formatted !== null) {
                        $ueuseItems[] = $formatted;
                    }
                }

                if($message['ads'] === "true"){
                    $adsystem = array(
                        "type" => "Ads",
                        "url" => $message['ads_url'],
                        "imgurl" => $message['ads_img_url'],
                        "memo" => $message['ads_memo'],
                    );
                }else{
                    $adsystem = null;
                }
            }else{
                $adsystem = null;
            }

            $userItems = array();
            if(!empty($users)){
                foreach ($users as $value) {
                    $follower = $value['follower'];
                    $followerIds = array_reverse(array_values(array_filter(explode(',', $follower))));
                    $followerCount = count($followerIds);

                    $follow = $value['follow'];
                    $followIds = array_reverse(array_values(array_filter(explode(',', $follow))));
                    $followCount = count($followIds);

                    $user = array(
                        "type" => "User",
                        "userid" => $value["userid"],
                        "userdata" => array(
                            "userid" => $value["userid"],
                            "username" => $value["username"],
                            "headurl" => filter_var($value['headname'], FILTER_VALIDATE_URL) 
                                                ? $value['headname'] 
                                                : "../" . $value['headname'],
                            "iconurl" => filter_var($value['iconname'], FILTER_VALIDATE_URL) 
                                                ? $value['iconname'] 
                                                : "../" . $value['iconname'],
                            "role" => $value["role"],
                            "sacinfo" => $value["sacinfo"],
                            "follower_cnt" => $followerCount,
                            "follow_cnt" => $followCount,
                            "datetime" => $value["datetime"],
                            "profile" => $value["profile"],
                        ),
                    );

                    $userItems[] = $user;
                }
            }

            $ueuses = array_merge($userItems, $ueuseItems);
            if (empty($ueuses)) {
                $item = array(
                    "success" => false,
                    "ueuses" => null,
                    "ads" => null,
                    "error" => "no_ueuse",
                );
                echo json_encode($item, JSON_UNESCAPED_UNICODE);
            }else{
                $item = array(
                    "success" => true,
                    "ueuses" => $ueuses,
                    "ads" => $adsystem,
                );
        
                echo json_encode($item, JSON_UNESCAPED_UNICODE); 
            }
        
            $pdo = null;
        }else{
            $item = array(
                "success" => false,
                "ueuses" => null,
                "ads" => null,
                "error" => "no_ueuse",
            );
            echo json_encode($item, JSON_UNESCAPED_UNICODE);
        }
    }
}else{
    $item = array(
        "success" => false,
        "ueuses" => null,
        "ads" => null,
        "error" => "bad_request",
    );
    echo json_encode($item, JSON_UNESCAPED_UNICODE);
}
?>
