<?php

namespace App\Models;

use App\Core\Model;

class MemberProfile extends Model
{
    protected $table = 'member_profiles';
    protected $primaryKey = 'id';

    public function findByUser($userId)
    {
        $stmt = $this->db->prepare("SELECT * FROM {$this->table} WHERE user_id = ?");
        $stmt->execute([$userId]);
        return $stmt->fetch(\PDO::FETCH_OBJ);
    }

    public function getCompletionPercentage($userId)
    {
        $profile = $this->findByUser($userId);
        if (!$profile) return 0;

        $fields = [
            'gender', 'nationality', 'medical_council_number', 'postal_address',
            'residential_address', 'mobile_phone', 'alternative_phone',
            'institution_hospital', 'city_town', 'country', 'years_practice',
            'highest_qualification', 'membership_category', 'declaration_initials', 'declaration_date'
        ];

        $filled = 0;
        foreach ($fields as $field) {
            if (!empty($profile->$field)) $filled++;
        }

        return round(($filled / count($fields)) * 100);
    }

    public function isComplete($userId)
    {
        return $this->getCompletionPercentage($userId) >= 100;
    }
}
