const BASE = () => process.env.HA_URL || 'http://192.168.1.4:8123';
let _token = null;

export function initHA(token) {
  _token = token;
  console.log('Home Assistant: ready');
}

function token() {
  if (!_token) throw new Error('HA not initialized');
  return _token;
}

async function api(path, opts = {}) {
  const res = await fetch(`${BASE()}/api${path}`, {
    ...opts,
    headers: { Authorization: `Bearer ${token()}`, 'Content-Type': 'application/json', ...opts.headers },
  });
  if (!res.ok) throw new Error(`HA API ${path}: ${res.status}`);
  return res.json();
}

export async function haGetState(entityId) {
  const s = await api(`/states/${entityId}`);
  return { entity_id: s.entity_id, state: s.state, attributes: s.attributes, last_changed: s.last_changed };
}

export async function haListEntities(domain) {
  const states = await api('/states');
  const filtered = domain ? states.filter(s => s.entity_id.startsWith(`${domain}.`)) : states;
  return filtered.map(s => ({ entity_id: s.entity_id, state: s.state, friendly_name: s.attributes.friendly_name }));
}

export async function haCallService(domain, service, data = {}) {
  return api(`/services/${domain}/${service}`, {
    method: 'POST',
    body: JSON.stringify(data),
  });
}

export async function haGetHistory(entityId, hours = 24) {
  const start = new Date(Date.now() - hours * 3600 * 1000).toISOString();
  const data = await api(`/history/period/${start}?filter_entity_id=${entityId}&minimal_response=true`);
  return data[0] || [];
}
