package com.ttstd.dialer.db.app;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import java.util.List;

@Dao
public interface AppDao {
    // 基本查询：获取总行数
    @Query("SELECT COUNT(*) FROM app_list")
    Integer getTotalCount();

    // 查询：根据特定列的非空值计数
    @Query("SELECT COUNT(id) FROM app_list")
    Integer getCountById();

    @Query("SELECT * FROM app_list WHERE package_name = :packageName AND class_name = :className")
    AppInfo getAppInfo(String packageName, String className);

    @Query("SELECT id FROM app_list WHERE package_name = :packageName AND class_name = :className")
    Integer getIdByPackageAndClass(String packageName, String className);

    // 检查数据是否存在（返回布尔值）
    @Query("SELECT COUNT(*) FROM app_list WHERE package_name = :pkgName AND class_name = :clsName")
    Integer checkAppInfoExists(String pkgName, String clsName);

    @Insert
    long insert(AppInfo appInfo);

    @Insert
    long[] insert(List<AppInfo> appInfos);

    @Update
    Integer update(AppInfo appInfo);

    @Delete
    Integer delete(AppInfo appInfo);

    @Query("DELETE FROM app_list WHERE id = :id")
    Integer deleteById(Integer id);

    @Query("DELETE FROM app_list")
    Integer deleteAll();

    @Query("SELECT * FROM app_list ORDER BY position ASC")
    List<AppInfo> getAllApp();

    @Query("SELECT * FROM app_list WHERE outside = 1 ORDER BY position ASC")
    List<AppInfo> getOutsideApp();

    @Query("SELECT * FROM app_list WHERE outside = 0 ORDER BY position ASC")
    List<AppInfo> getInsideApp();

    @Query("SELECT * FROM app_list WHERE id = :id")
    AppInfo getAppById(Integer id);

    @Query("SELECT * FROM app_list WHERE label LIKE :searchQuery OR package_name LIKE :searchQuery")
    List<AppInfo> searchApp(String searchQuery);
}
