#    Copyright (C) 2025 James Brotosky, Brandon Wickline
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <https://www.gnu.org/licenses/>.

from dataclasses import dataclass, field
from typing import ClassVar, List, Optional

from models.policy import Policy


@dataclass
class Agent:
    hostname: str
    agentid: str
    clientversion: str
    domain: str
    freespace: int
    groupid: str
    ip: str
    localip: str
    lastcheckin: str
    os: str
    policyversion: str
    status: int
    username: str
    groupname: Optional[str] = field(default=None)
    status_text: Optional[str] = field(default=None)

    # Class-level status map
    status_map: ClassVar[dict] = {0: "Offline", 1: "Online", 2: "Hidden", 3: "Safemode"}

    def enrich_with_policies(self, policies: List[Policy]):
        """Enrich the agent with groupname and human-readable status."""
        self.status_text = self.status_map.get(self.status, "Unknown")
        for policy in policies:
            if policy.groupid == self.groupid:
                self.groupname = policy.name
                break
        if not self.groupname:
            self.groupname = "Unknown"


"""

from models.agent import Agent
from modesls.policy

# Step 1: Load data from API
policies = [Policy(**row['data']) for _, row in api.policy_find_all().iterrows()]
agents = [Agent(**row['data']) for _, row in api.agent_find_all().iterrows()]

# Step 2: Create groupid → groupname map
groupid_to_name = {policy.groupid: policy.name for policy in policies}

# Step 3: Enrich agents
for agent in agents:
    agent.enrich_with_policies(groupid_to_name)


"""
