<?php

require_once __DIR__ . '/../../src/Database/Database.php';

class Injury
{
    private $id;
    private $injuryType;
    private $description;
    private $category;
    private $severity;
    private $statModifications = [];

    private static $cureMap = [
        'wound' => 'basic_medicine',
        'fracture' => 'bone_setting',
        'infection' => 'healing_herbs',
        'poison' => 'antidote',
        'burn' => 'burn_salve',
        'malnourished' => 'food',     // Cured by having enough food
        'old-age' => null // Cannot be cured
    ];

    public function __construct($id, $injuryType, $description, $category, $severity, $statModifications = [])
    {
        $this->id = $id;
        $this->description = $description;
        $this->injuryType = $injuryType;
        $this->category = $category;
        $this->severity = $severity;
        $this->statModifications = $statModifications;
    }

    public static function load($id)
    {
        $db = Database::getInstance();
        $pdo = $db->getPDO();

        $stmt = $pdo->prepare("SELECT * FROM injuries WHERE id = ?");
        $stmt->execute([$id]);
        $row = $stmt->fetch();

        if (!$row) {
            return null;
        }

        return new self($row['id'], $row['kobold_id'], $row['injury_type'], $row['category'], $row['severity']);
    }

    public function getId()
    {
        return $this->id;
    }

    public function getInjuryType()
    {
        return $this->injuryType;
    }

    public function getCategory()
    {
        return $this->category;
    }

    public function getSeverity()
    {
        return $this->severity;
    }

    public function canBeCured()
    {
        return isset(self::$cureMap[$this->category]) && self::$cureMap[$this->category] !== null;
    }

    public function getRequiredMedicine()
    {
        return self::$cureMap[$this->category] ?? null;
    }

    public static function getCureForCategory($category)
    {
        return self::$cureMap[$category] ?? null;
    }

    public function getDescription()
    {
        return ucfirst(str_replace('_', ' ', $this->injuryType));
    }

    public function delete()
    {
        $db = Database::getInstance();
        $pdo = $db->getPDO();

        $stmt = $pdo->prepare("DELETE FROM injuries WHERE id = ?");
        $stmt->execute([$this->id]);
    }
}
