<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/

class Core_Model_DbTable_Saveditems extends Engine_Db_Table
{

  protected $_rowClass = "Core_Model_Saveditem";
  protected $_name = 'core_saveditems';

  /**
   * Get a single saved item by user, content type, and content id.
   *
   * @param int $userId
   * @param string $contentType
   * @param int $contentId
   * @return Core_Model_Saveditem|null
   */
  public function getSavedItem($userId, $contentType, $contentId)
  {
    $select = $this->select()
      ->where('user_id = ?', $userId)
      ->where('content_type = ?', $contentType)
      ->where('content_id = ?', $contentId)
      ->limit(1);
    return $this->fetchRow($select);
  }

  /**
   * Get a Zend_Db_Select for the user's saved items, with optional filtering/sorting.
   *
   * @param int $userId
   * @param array $params [search, sort, content_type]
   * @return Zend_Db_Table_Select
   */
  public function getSavedItemsSelect($userId, array $params = [])
  {
    $select = $this->select()
      ->from($this->info('name'))
      ->where('user_id = ?', $userId);

    // Filter by content_type if provided
    if (!empty($params['content_type'])) {
      $select->where('content_type = ?', $params['content_type']);
    }

    // Search filter: match against content_type (since title is resolved at runtime)
    if (!empty($params['search'])) {
      $searchTerm = '%' . strtolower(trim($params['search'])) . '%';
      $select->where('LOWER(content_type) LIKE ?', $searchTerm);
    }

    // Sort
    $sort = isset($params['sort']) ? $params['sort'] : 'date_newest';
    switch ($sort) {
      case 'date_oldest':
        $select->order('created_at ASC');
        break;
      case 'alpha_asc':
        $select->order('content_type ASC');
        $select->order('content_id ASC');
        break;
      case 'alpha_desc':
        $select->order('content_type DESC');
        $select->order('content_id DESC');
        break;
      case 'date_newest':
      default:
        $select->order('created_at DESC');
        break;
    }

    return $select;
  }

  /**
   * Check if a content item is saved by a given user.
   *
   * @param int $userId
   * @param string $contentType
   * @param int $contentId
   * @return bool
   */
  public function isItemSaved($userId, $contentType, $contentId): bool
  {
    $select = $this->select()
      ->from($this->info('name'), array('saveditem_id'))
      ->where('user_id = ?', $userId)
      ->where('content_type = ?', $contentType)
      ->where('content_id = ?', $contentId)
      ->limit(1);
    return (bool) $select->query()->fetchColumn();
  }

  /**
   * Delete all saved items for a specific content item.
   *
   * @param string $contentType
   * @param int $contentId
   * @return int Number of rows deleted
   */
  public function deleteByContent($contentType, $contentId): int
  {
    return $this->delete(array(
      'content_type = ?' => $contentType,
      'content_id = ?' => $contentId,
    ));
  }

  /**
   * Delete all saved items for a specific user.
   *
   * @param int $userId
   * @return int Number of rows deleted
   */
  public function deleteByUser($userId): int
  {
    return $this->delete(array(
      'user_id = ?' => $userId,
    ));
  }
}
