package com.lock.arcanvasart.room;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;

import java.util.List;

@Dao
public interface FavoriteImageDao {

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    void insertAll(List<FavoriteImage> images);

    @Update
    void update(FavoriteImage image);

    @Query("SELECT * FROM favorite_images WHERE is_favorite = 1")
    LiveData<List<FavoriteImage>> getFavoriteImages();

    @Query("SELECT * FROM favorite_images WHERE image_path = :path LIMIT 1")
    LiveData<FavoriteImage> getImageByPath(String path);

    @Query("SELECT * FROM favorite_images WHERE is_favorite = 1")
    LiveData<List<FavoriteImage>> getAllFavoriteImages();

    @Query("UPDATE favorite_images SET is_favorite = :isFavorite WHERE image_path = :imagePath")
    void updateFavoriteStatusByPath(String imagePath, boolean isFavorite);

    @Query("SELECT * FROM favorite_images WHERE image_path LIKE :category || '/%'")
    LiveData<List<FavoriteImage>> getImagesByCategory(String category);

    @Query("SELECT * FROM favorite_images ORDER BY RANDOM() LIMIT 20")
    LiveData<List<FavoriteImage>> getRandomImagesForRecyclerView();

    @Query("SELECT * FROM favorite_images ORDER BY RANDOM() LIMIT 4")
    List<FavoriteImage> getRandomImagesForCarousel();

    @Query("SELECT * FROM favorite_images ORDER BY RANDOM() LIMIT 20")
    LiveData<List<FavoriteImage>> getInitialRandomImagesForRecyclerView();

    @Query("SELECT * FROM favorite_images LIMIT :limit OFFSET :offset")
    LiveData<List<FavoriteImage>> getImagesForRecyclerView(int limit, int offset);

    // 随机获取20张图片
    @Query("SELECT * FROM favorite_images ORDER BY RANDOM() LIMIT :count")
    List<FavoriteImage> getRandomFavoriteImagesSync(int count);

    // 更新图片的收藏状态
    @Query("UPDATE favorite_images SET is_favorite = :isFavorite WHERE id = :imageId")
    void updateFavoriteStatus(int imageId, boolean isFavorite);
}

