<?php

if(!defined('SOFTACULOUS')){
	die('Hacking Attempt');
}

require_once(__DIR__ . '/class_ai_file_handler.php');

class AIStats {

	public static function get_stats_dir($username){
		$base = AIFileHandler::get_ai_base_dir($username);
		$dir = $base . '/stats';
		AIFileHandler::ensure_dir($dir, true);
		return $dir;
	}

	public static function increment_provider($username, $provider){
		if(empty($provider)) return false;
		$dir = self::get_stats_dir($username);
		$file = $dir . '/provider_usage.json.php';
		$data = AIFileHandler::read($file);
		if(!$data) $data = array();
		if(!isset($data[$provider])){
			$data[$provider] = array('messages' => 0, 'conversations' => 0, 'last_used' => 0);
		}
		$data[$provider]['messages']++;
		$data[$provider]['last_used'] = time();
		return AIFileHandler::write($file, $data);
	}

	public static function increment_provider_conversation($username, $provider){
		if(empty($provider)) return false;
		$dir = self::get_stats_dir($username);
		$file = $dir . '/provider_usage.json.php';
		$data = AIFileHandler::read($file);
		if(!$data) $data = array();
		if(!isset($data[$provider])){
			$data[$provider] = array('messages' => 0, 'conversations' => 0, 'last_used' => 0);
		}
		$data[$provider]['conversations']++;
		$data[$provider]['last_used'] = time();
		return AIFileHandler::write($file, $data);
	}
}