<?php

namespace App\Core;

use PDO;

/**
 * نظام مصادقة الأطباء - تسجيل دخول منفصل للوحة تحكم الطبيب
 * يستخدم جدول doctor_accounts (ربط doctor_id + tenant_id)
 */
class DoctorAuth
{
    private PDO $db;

    public function __construct(PDO $db)
    {
        if (session_status() === PHP_SESSION_NONE) {
            session_start();
        }
        $this->db = $db;
    }

    public function attempt(string $username, string $password): bool
    {
        try {
            $this->db->query('SELECT id FROM doctor_accounts LIMIT 1');
        } catch (\Throwable $e) {
            return false;
        }

        $stmt = $this->db->prepare(
            'SELECT da.*, d.full_name, d.specialty, d.clinic_name, t.name AS tenant_name, t.slug AS tenant_slug
             FROM doctor_accounts da
             JOIN doctors d ON d.id = da.doctor_id AND d.tenant_id = da.tenant_id
             JOIN tenants t ON t.id = da.tenant_id
             WHERE da.username = ? AND da.is_active = 1 AND t.status IN ("active","trial")'
        );
        $stmt->execute([$username]);
        $account = $stmt->fetch();

        if (!$account || !password_verify($password, $account['password_hash'])) {
            return false;
        }

        $_SESSION['doctor_account_id'] = (int)$account['id'];
        $_SESSION['doctor_id'] = (int)$account['doctor_id'];
        $_SESSION['doctor_tenant_id'] = (int)$account['tenant_id'];
        $_SESSION['doctor_name'] = $account['full_name'];
        $_SESSION['doctor_specialty'] = $account['specialty'] ?? '';
        $_SESSION['doctor_clinic'] = $account['clinic_name'] ?? '';
        $_SESSION['doctor_tenant_name'] = $account['tenant_name'] ?? '';
        $_SESSION['doctor_tenant_slug'] = $account['tenant_slug'] ?? '';
        $_SESSION['doctor_last_activity'] = time();

        $this->db->prepare('UPDATE doctor_accounts SET last_login_at = NOW() WHERE id = ?')
            ->execute([$account['id']]);

        return true;
    }

    public function check(): bool
    {
        if (session_status() === PHP_SESSION_NONE) {
            session_start();
        }
        if (!isset($_SESSION['doctor_account_id'])) {
            return false;
        }
        $timeout = 30 * 60; // 30 دقيقة
        if (isset($_SESSION['doctor_last_activity']) && (time() - (int)$_SESSION['doctor_last_activity']) > $timeout) {
            $this->logout();
            return false;
        }
        $_SESSION['doctor_last_activity'] = time();
        return true;
    }

    public function logout(): void
    {
        if (session_status() === PHP_SESSION_NONE) {
            session_start();
        }
        $keys = ['doctor_account_id', 'doctor_id', 'doctor_tenant_id', 'doctor_name', 'doctor_specialty', 'doctor_clinic', 'doctor_tenant_name', 'doctor_tenant_slug', 'doctor_last_activity'];
        foreach ($keys as $k) {
            unset($_SESSION[$k]);
        }
    }

    public function doctorId(): ?int
    {
        return $this->check() ? (int)($_SESSION['doctor_id'] ?? 0) : null;
    }

    public function tenantId(): ?int
    {
        return $this->check() ? (int)($_SESSION['doctor_tenant_id'] ?? 0) : null;
    }

    public function accountId(): ?int
    {
        return $this->check() ? (int)($_SESSION['doctor_account_id'] ?? 0) : null;
    }
}
