<?php

namespace ZenoChatbot\Model;

use ZenoChatbot\Framework\Database\Model;

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
 * SavedReply Model
 *
 * Represents predefined question-answer pairs for quick responses
 */
class SavedReply extends Model
{
    /**
     * Table name
     */
    protected $table = 'zenochatbot_saved_replies';

    /**
     * Primary key
     */
    protected $primaryKey = 'id';

    /**
     * Enable timestamps
     */
    public $timestamps = true;

    /**
     * Fillable attributes
     */
    protected $fillable = [
        'question',
        'answer',
        'match_type',
        'keywords',
        'language',
        'priority',
        'hit_count',
        'embedding',
        'embedding_provider',
        'is_active',
        'created_at',
        'updated_at'
    ];

    /**
     * Attributes that should be cast
     */
    protected $casts = [
        'id' => 'int',
        'priority' => 'int',
        'hit_count' => 'int',
        'is_active' => 'bool',
        'created_at' => 'datetime',
        'updated_at' => 'datetime'
    ];

    /**
     * Scope: Only active replies
     */
    public static function active()
    {
        return static::where('is_active', 1);
    }

    /**
     * Increment hit count when reply is used
     */
    public function incrementHitCount()
    {
        $this->hit_count = ($this->hit_count ?? 0) + 1;
        return $this->save();
    }
}

