<?php

namespace App\Modules\Notification\Services;

use App\Modules\Academic\Models\StudentModel;
use App\Modules\Notification\Models\StudentParentModel;
use App\Modules\Notification\Models\TelegramAccountModel;

/**
 * Parent Link Service
 * 
 * Handles parent linking logic for students via Telegram.
 */
class ParentLinkService
{
    protected StudentModel $studentModel;
    protected TelegramAccountModel $telegramAccountModel;
    protected StudentParentModel $studentParentModel;

    /**
     * Link code length
     */
    protected int $linkCodeLength = 16;

    public function __construct()
    {
        $this->studentModel = new StudentModel();
        $this->telegramAccountModel = new TelegramAccountModel();
        $this->studentParentModel = new StudentParentModel();
    }

    /**
     * Generate unique link code for student
     * 
     * @param int $studentId Student ID
     * @return string Generated link code
     */
    public function generateLinkCode(int $studentId): string
    {
        // Check if student exists
        $student = $this->studentModel->find($studentId);
        if (!$student) {
            throw new \InvalidArgumentException("Student with ID {$studentId} not found");
        }

        // Generate unique link code
        do {
            $linkCode = $this->generateRandomCode($this->linkCodeLength);
            $existing = $this->studentModel->where('parent_link_code', $linkCode)->first();
        } while ($existing !== null);

        // Update student with link code
        $this->studentModel->update($studentId, [
            'parent_link_code' => $linkCode,
        ]);

        return $linkCode;
    }

    /**
     * Link Telegram account to student using link code
     * 
     * @param string $linkCode Link code from student
     * @param int $telegramUserId Telegram user ID
     * @param array $profileData Telegram profile data (username, first_name, last_name)
     * @return array Result with student_id, parent_id, telegram_account_id
     */
    public function linkTelegramToStudent(string $linkCode, int $telegramUserId, array $profileData): array
    {
        // Find student by link code
        $student = $this->studentModel->where('parent_link_code', $linkCode)->first();
        
        if (!$student) {
            throw new \InvalidArgumentException("Invalid link code");
        }

        // Check if Telegram account already exists
        $telegramAccount = $this->telegramAccountModel->findByTelegramUserId($telegramUserId);

        // Create parent first so we can set parent_id on telegram account
        $parentModel = new \App\Modules\Notification\Models\ParentModel();
        $parentId = $parentModel->insert([
            'name'         => trim(($profileData['first_name'] ?? '') . ' ' . ($profileData['last_name'] ?? '')),
            'phone_number' => null, // Can be updated later
        ]);

        if (!$telegramAccount) {
            // Create new Telegram account linked to parent
            $telegramAccountId = $this->telegramAccountModel->insert([
                'telegram_user_id' => $telegramUserId,
                'username'         => $profileData['username'] ?? null,
                'first_name'       => $profileData['first_name'] ?? null,
                'last_name'        => $profileData['last_name'] ?? null,
                'is_verified'      => 1,
                'parent_id'        => $parentId,
            ]);
        } else {
            $telegramAccountId = $telegramAccount->id;
            // Update profile data and link to parent
            $this->telegramAccountModel->update($telegramAccountId, [
                'username'   => $profileData['username'] ?? $telegramAccount->username,
                'first_name' => $profileData['first_name'] ?? $telegramAccount->first_name,
                'last_name'  => $profileData['last_name'] ?? $telegramAccount->last_name,
                'is_verified' => 1,
                'parent_id'   => $parentId,
            ]);
        }

        // Create student-parent relationship
        $studentParentId = $this->studentParentModel->insert([
            'student_id'   => $student->id,
            'parent_id'    => $parentId,
            'relationship' => 'WALI',
        ]);

        // Clear link code after successful linking
        $this->studentModel->update($student->id, [
            'parent_link_code' => null,
        ]);

        return [
            'student_id'          => $student->id,
            'parent_id'           => $parentId,
            'telegram_account_id' => $telegramAccountId,
            'student_parent_id'   => $studentParentId,
        ];
    }

    /**
     * Generate random alphanumeric code
     * 
     * @param int $length Code length
     * @return string Generated code
     */
    protected function generateRandomCode(int $length): string
    {
        $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $code = '';
        
        for ($i = 0; $i < $length; $i++) {
            $code .= $characters[random_int(0, strlen($characters) - 1)];
        }
        
        return $code;
    }
}
