<?php
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Conversation Model
 *
 * Database operations for chatbot conversations
 *
 * @package    Kitsune_AI_Platform
 * @subpackage Kitsune_AI_Platform/includes/models
 */

class Kitsune_AI_Platform_Conversation_Model {

    private $table_name;

    public function __construct() {
        global $wpdb;
        $this->table_name = $wpdb->prefix . 'kitsune_ai_platform_conversations';
    }

    public function get_by_id($id) {
        global $wpdb;
        return $wpdb->get_row($wpdb->prepare("SELECT * FROM {$this->table_name} WHERE id = %d", $id));
    }

    public function get_by_identifier($chatbot_id, $identifier) {
        global $wpdb;
        return $wpdb->get_row(
            $wpdb->prepare(
                "SELECT * FROM {$this->table_name} WHERE chatbot_id = %d AND user_identifier = %s ORDER BY id DESC LIMIT 1",
                $chatbot_id,
                $identifier
            )
        );
    }

    public function create($data) {
        global $wpdb;
        $wpdb->insert($this->table_name, $data);
        return $wpdb->insert_id;
    }

    public function update($id, $data) {
        global $wpdb;
        return $wpdb->update($this->table_name, $data, array('id' => $id));
    }

    public function delete($id) {
        global $wpdb;
        return $wpdb->delete($this->table_name, array('id' => $id));
    }
}
