<?php
/**
 * NuriBoard - 회원 라우트
 * 프로필, 공개 프로필, 출석체크, 쪽지 관련 라우트
 */

// 프로필
Router::get('/profile', function () {
    Auth::requireLogin();
    $member = Member::find(Auth::id());
    Router::loadTheme('member/profile', ['member' => $member]);
});

// 공개 프로필
Router::get('/member/{id}', function ($params) {
    $member = Member::find((int)$params['id']);
    if (!$member) { http_response_code(404); Router::loadTheme('error/404'); return; }
    Router::loadTheme('member/public', ['member' => $member]);
});

// 팔로워/팔로잉 목록
Router::get('/member/{id}/followers', function ($params) {
    $member = Member::find((int)$params['id']);
    if (!$member) { http_response_code(404); Router::loadTheme('error/404'); return; }
    Follow::ensureTable();
    $page = max(1, (int)($_GET['p'] ?? 1));
    $data = Follow::listFollowers((int)$member['id'], $page);
    Router::loadTheme('member/followers', ['member' => $member, 'tab' => 'followers', 'data' => $data]);
});
Router::get('/member/{id}/following', function ($params) {
    $member = Member::find((int)$params['id']);
    if (!$member) { http_response_code(404); Router::loadTheme('error/404'); return; }
    Follow::ensureTable();
    $page = max(1, (int)($_GET['p'] ?? 1));
    $data = Follow::listFollowing((int)$member['id'], $page);
    Router::loadTheme('member/followers', ['member' => $member, 'tab' => 'following', 'data' => $data]);
});

// 출석체크
Router::get('/attendance', function () {
    Auth::requireLogin();
    $prefix = DB::getPrefix();
    $year = (int)($_GET['y'] ?? date('Y'));
    $month = (int)($_GET['m'] ?? date('m'));
    // 이번 달 출석 기록
    $myAttend = DB::fetchAll("SELECT attend_date FROM {$prefix}attendance WHERE member_id = ? AND attend_date LIKE ?", [Auth::id(), "{$year}-" . str_pad($month, 2, '0', STR_PAD_LEFT) . "-%"]);
    $attendDates = array_column($myAttend, 'attend_date');
    // 오늘 출석 여부
    $todayDone = in_array(date('Y-m-d'), $attendDates);
    // 연속 출석일 계산
    $streak = 0;
    $d = date('Y-m-d');
    while (true) {
        $exists = DB::fetch("SELECT id FROM {$prefix}attendance WHERE member_id = ? AND attend_date = ?", [Auth::id(), $d]);
        if (!$exists) break;
        $streak++;
        $d = date('Y-m-d', strtotime($d . ' -1 day'));
    }
    // 오늘 출석 랭킹
    $todayList = DB::fetchAll("SELECT a.*, m.nickname, m.level FROM {$prefix}attendance a LEFT JOIN {$prefix}members m ON a.member_id = m.id WHERE a.attend_date = CURDATE() ORDER BY a.created_at ASC");
    Router::loadTheme('member/attendance', [
        'year' => $year, 'month' => $month,
        'attendDates' => $attendDates, 'todayDone' => $todayDone,
        'streak' => $streak, 'todayList' => $todayList,
    ]);
});
Router::post('/attendance', function () {
    Auth::requireLogin();
    if (!Auth::verifyCsrf()) die('잘못된 요청');
    $prefix = DB::getPrefix();
    $today = date('Y-m-d');
    // 중복 체크
    $exists = DB::fetch("SELECT id FROM {$prefix}attendance WHERE member_id = ? AND attend_date = ?", [Auth::id(), $today]);
    if ($exists) { Router::redirect(nb_url('attendance')); return; }
    $msg = trim($_POST['message'] ?? '출석합니다!');
    DB::insert("{$prefix}attendance", [
        'member_id' => Auth::id(),
        'message' => mb_strimwidth($msg, 0, 100, ''),
        'attend_date' => $today,
    ]);
    // 포인트 지급
    $attendPoint = (int)nb_setting('point_attendance', '5');
    if ($attendPoint > 0) Point::give(Auth::id(), $attendPoint, '출석체크');
    // 연속 출석 보너스 (7일마다)
    $streak = 0;
    $d = $today;
    while (true) {
        $e = DB::fetch("SELECT id FROM {$prefix}attendance WHERE member_id = ? AND attend_date = ?", [Auth::id(), $d]);
        if (!$e) break;
        $streak++;
        $d = date('Y-m-d', strtotime($d . ' -1 day'));
    }
    $bonusPoint = (int)nb_setting('point_attendance_bonus', '20');
    if ($streak > 0 && $streak % 7 === 0 && $bonusPoint > 0) {
        Point::give(Auth::id(), $bonusPoint, "연속 {$streak}일 출석 보너스");
    }
    Cache::flush();
    Router::redirect(nb_url('attendance'));
});

Router::post('/profile', function () {
    Auth::requireLogin();
    if (!Auth::verifyCsrf()) die('잘못된 요청입니다.');
    $data = ['nickname' => trim($_POST['nickname'] ?? ''), 'email' => trim($_POST['email'] ?? '')];
    if (!empty($_POST['password'])) $data['password'] = $_POST['password'];

    // 프로필 이미지 삭제
    if (!empty($_POST['delete_profile_image'])) {
        $current = Member::find(Auth::id())['profile_image'] ?? '';
        if ($current && file_exists(NB_ROOT . '/' . $current)) unlink(NB_ROOT . '/' . $current);
        $data['profile_image'] = '';
    }

    // 프로필 이미지 업로드
    if (!empty($_FILES['profile_image']) && $_FILES['profile_image']['error'] === UPLOAD_ERR_OK) {
        $ext = strtolower(pathinfo($_FILES['profile_image']['name'], PATHINFO_EXTENSION));
        if (in_array($ext, ['jpg','jpeg','png','gif','webp'])) {
            // 기존 이미지 삭제
            $current = Member::find(Auth::id())['profile_image'] ?? '';
            if ($current && file_exists(NB_ROOT . '/' . $current)) unlink(NB_ROOT . '/' . $current);
            $dir = NB_ROOT . '/uploads/profile';
            if (!is_dir($dir)) mkdir($dir, 0755, true);
            $newName = 'pf_' . Auth::id() . '_' . time() . '.' . $ext;
            $savePath = $dir . '/' . $newName;
            move_uploaded_file($_FILES['profile_image']['tmp_name'], $savePath);
            if ($ext !== 'webp' && function_exists('imagewebp')) {
                $webpPath = Upload::convertToWebpPublic($savePath, $ext);
                if ($webpPath) $newName = preg_replace('/\.[^.]+$/', '.webp', $newName);
            }
            $data['profile_image'] = 'uploads/profile/' . $newName;
        }
    }

    Member::update(Auth::id(), $data);
    // 세션 갱신
    $_SESSION['member']['nickname'] = $data['nickname'];
    $_SESSION['member']['email'] = $data['email'] ?? '';
    $_SESSION['member']['profile_image'] = $data['profile_image'] ?? ($_SESSION['member']['profile_image'] ?? '');
    Router::redirect(nb_url('profile') . '?tab=info&saved=1');
});

// ===== 쪽지 =====

// 쪽지함
Router::get('/messages', function () {
    Auth::requireLogin();
    $box  = $_GET['box'] ?? 'inbox';
    $page = max(1, (int)($_GET['p'] ?? 1));
    $data = $box === 'sent'
        ? Message::outbox(Auth::id(), $page)
        : Message::inbox(Auth::id(), $page);
    Router::loadTheme('member/messages', ['box' => $box, 'data' => $data]);
});

// 쪽지 보내기 (폼 페이지) — 고정 경로는 {id} 와일드카드보다 먼저 등록
Router::get('/messages/write', function () {
    Auth::requireLogin();
    $to = $_GET['to'] ?? '';
    Router::loadTheme('member/messages', ['write' => true, 'to' => $to]);
});

// 읽지 않은 쪽지 수 API
Router::get('/messages/unread-count', function () {
    Auth::requireLogin();
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode(['count' => Message::unreadCount(Auth::id())]);
    exit;
});

// 쪽지 전송 처리
Router::post('/messages/send', function () {
    Auth::requireLogin();
    header('Content-Type: application/json; charset=utf-8');
    if (!Auth::verifyCsrf()) { echo json_encode(['success' => false, 'message' => '잘못된 요청']); exit; }

    $toNick  = trim($_POST['to'] ?? '');
    $title   = trim($_POST['title'] ?? '');
    $content = trim($_POST['content'] ?? '');

    if (!$toNick || !$title || !$content) {
        echo json_encode(['success' => false, 'message' => '모든 항목을 입력하세요.']); exit;
    }
    $receiver = Message::findMemberByNickname($toNick);
    if (!$receiver) {
        echo json_encode(['success' => false, 'message' => '존재하지 않는 닉네임입니다.']); exit;
    }
    if ($receiver['id'] === Auth::id()) {
        echo json_encode(['success' => false, 'message' => '자신에게는 쪽지를 보낼 수 없습니다.']); exit;
    }
    // 쪽지 포인트 소모 체크
    $msgCost = (int)nb_setting('point_message', '0');
    if ($msgCost > 0 && !Auth::isAdmin()) {
        $myPoint = (int)(Auth::user()['point'] ?? 0);
        if ($myPoint < $msgCost) {
            echo json_encode(['success' => false, 'message' => "포인트가 부족합니다. (필요: {$msgCost}P, 보유: {$myPoint}P)"]); exit;
        }
        Point::give(Auth::id(), -$msgCost, '쪽지 발송');
    }
    Message::send(Auth::id(), $receiver['id'], $title, $content);
    echo json_encode(['success' => true, 'message' => '쪽지를 보냈습니다.']);
    exit;
});

// 쪽지 상세 + 읽음 처리 — 와일드카드는 고정 경로 이후에 등록
Router::get('/messages/{id}', function ($params) {
    Auth::requireLogin();
    $msg = Message::find((int)$params['id']);
    if (!$msg) { http_response_code(404); return; }
    if ($msg['receiver_id'] !== Auth::id() && $msg['sender_id'] !== Auth::id()) {
        http_response_code(403); return;
    }
    if ($msg['receiver_id'] === Auth::id() && !$msg['is_read']) {
        Message::markRead((int)$params['id']);
        $msg['is_read'] = 1;
    }
    Router::loadTheme('member/messages', ['view' => $msg, 'box' => $_GET['box'] ?? 'inbox']);
});

// 쪽지 삭제
Router::post('/messages/{id}/delete', function ($params) {
    Auth::requireLogin();
    if (!Auth::verifyCsrf()) die('잘못된 요청');
    $msg = Message::find((int)$params['id']);
    if (!$msg) { http_response_code(404); return; }
    $box = $_POST['box'] ?? 'inbox';
    if ($msg['receiver_id'] === Auth::id()) {
        Message::deleteForReceiver((int)$params['id']);
    } elseif ($msg['sender_id'] === Auth::id()) {
        Message::deleteForSender((int)$params['id']);
    }
    Router::redirect(nb_url('messages?box=' . $box));
});
