<?php

class NPC
{
    private $id;
    private $saveId;
    private $name;
    private $npcType; // 'tribe' or 'individual'
    private $isRival;
    private $data = [];

    public function __construct($id, $saveId, $name, $npcType, $isRival = false)
    {
        $this->id = $id;
        $this->saveId = $saveId;
        $this->name = $name;
        $this->npcType = $npcType;
        $this->isRival = $isRival;
    }

    public static function create($saveId, $name, $npcType, $isRival = false)
    {
        $id = uniqid('npc_');
        return new self($id, $saveId, $name, $npcType, $isRival);
    }

    public static function fromArray($data)
    {
        $npc = new self(
            $data['id'] ?? uniqid('npc_'),
            $data['save_id'] ?? null,
            $data['name'] ?? '',
            $data['npc_type'] ?? 'individual',
            $data['is_rival'] ?? false
        );

        if (isset($data['data'])) {
            $npc->data = $data['data'];
        }

        return $npc;
    }

    public function toArray()
    {
        return [
            'id' => $this->id,
            'save_id' => $this->saveId,
            'name' => $this->name,
            'npc_type' => $this->npcType,
            'is_rival' => $this->isRival,
            'data' => $this->data
        ];
    }

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

    public function getName()
    {
        return $this->name;
    }

    public function getNpcType()
    {
        return $this->npcType;
    }

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

    public function setData($key, $value)
    {
        $this->data[$key] = $value;
    }

    public function getData($key = null)
    {
        if ($key === null) {
            return $this->data;
        }
        return $this->data[$key] ?? null;
    }

    public function save()
    {
        // NPCs are saved as part of the Tribe's npcs.json
    }
}
