<?php
namespace App\Modules\Health\Models;

class HealthProfile extends \Core\Model {
    protected $table = 'health_profiles';
    protected $moduleName = 'Health';

    public function getByUserId($userId) {
        $stmt = $this->query("SELECT * FROM {$this->table} WHERE user_id = ? AND deleted_at IS NULL LIMIT 1", [$userId]);
        return $stmt->fetch();
    }

    public function upsert($userId, $data) {
        $existing = $this->getByUserId($userId);
        
        if ($existing) {
            $sql = "UPDATE {$this->table} SET start_weight = ?, target_weight = ?, monthly_challenge_kg = ?, diabetes_type = ? WHERE user_id = ?";
            $this->query($sql, [$data['start_weight'], $data['target_weight'], $data['monthly_challenge_kg'], $data['diabetes_type'], $userId]);
        } else {
            $sql = "INSERT INTO {$this->table} (user_id, start_weight, target_weight, monthly_challenge_kg, diabetes_type) VALUES (?, ?, ?, ?, ?)";
            $this->query($sql, [$userId, $data['start_weight'], $data['target_weight'], $data['monthly_challenge_kg'], $data['diabetes_type']]);
        }
    }
}
