import { deleteLocalMovie, restoreLocalMovie, resetLocalMovie } from '../../../../lib/localDb.js'; import { txasupabase } from '../../../../lib/txasupabase.js'; export async function POST({ request, cookies }) { const token = cookies.get('auth_token')?.value; let user = null; if (token) { try { user = JSON.parse(Buffer.from(token, 'base64').toString('utf-8')); } catch (e) {} } if (!user || user.role !== 'admin') { return new Response( JSON.stringify({ error: 'Từ chối truy cập! Yêu cầu quyền quản trị viên.' }), { status: 403, headers: { 'Content-Type': 'application/json' } } ); } try { const { slugs, action, value } = await request.json(); if (!slugs || !Array.isArray(slugs) || slugs.length === 0) { return new Response( JSON.stringify({ error: 'Danh sách slug rỗng hoặc không hợp lệ!' }), { status: 400, headers: { 'Content-Type': 'application/json' } } ); } const validActions = ['delete', 'restore', 'reset', 'hard-delete', 'status', 'require-login', 'free-login']; if (!validActions.includes(action)) { return new Response( JSON.stringify({ error: 'Hành động hàng loạt không hợp lệ!' }), { status: 400, headers: { 'Content-Type': 'application/json' } } ); } let processedCount = 0; for (const slug of slugs) { if (action === 'delete') { await deleteLocalMovie(slug); processedCount++; } else if (action === 'restore') { const success = await restoreLocalMovie(slug); if (success) processedCount++; } else if (action === 'reset') { const success = await resetLocalMovie(slug); if (success) processedCount++; } else if (action === 'hard-delete') { // Xóa hoàn toàn phim khỏi cả bảng movies và deleted_movies await txasupabase.supabase.from('movies').delete().eq('slug', slug); await txasupabase.supabase.from('deleted_movies').delete().eq('slug', slug); processedCount++; } else if (action === 'status') { const movieObj = await txasupabase.getMovieBySlug(slug); if (movieObj) { movieObj.movie.status = value; // Cập nhật text hiển thị tương ứng if (value === 'completed') { movieObj.movie.episode_current = 'Hoàn Thành'; } else if (value === 'ongoing') { movieObj.movie.episode_current = 'Đang Chiếu'; } else if (value === 'trailer') { movieObj.movie.episode_current = 'Trailer / Sắp chiếu'; } await txasupabase.saveLocalMovie(slug, movieObj); processedCount++; } } else if (action === 'require-login') { const movieObj = await txasupabase.getMovieBySlug(slug); if (movieObj) { movieObj.movie.require_login = true; movieObj.movie.only_login = true; await txasupabase.saveLocalMovie(slug, movieObj); processedCount++; } } else if (action === 'free-login') { const movieObj = await txasupabase.getMovieBySlug(slug); if (movieObj) { movieObj.movie.require_login = false; movieObj.movie.only_login = false; await txasupabase.saveLocalMovie(slug, movieObj); processedCount++; } } } let message = ''; if (action === 'delete') { message = `Đã ẩn thành công ${processedCount} phim được chọn!`; } else if (action === 'restore') { message = `Đã khôi phục hiển thị thành công ${processedCount} phim được chọn!`; } else if (action === 'reset') { message = `Đã đặt lại thành công ${processedCount} phim được chọn về dữ liệu gốc!`; } else if (action === 'hard-delete') { message = `Đã xóa vĩnh viễn thành công ${processedCount} phim được chọn khỏi cơ sở dữ liệu!`; } else if (action === 'status') { const statusNames = { completed: 'Hoàn thành', ongoing: 'Đang chiếu', trailer: 'Sắp ra mắt' }; message = `Đã đổi nhanh trạng thái thành công cho ${processedCount} phim thành "${statusNames[value] || value}"!`; } else if (action === 'require-login') { message = `Đã thiết lập bắt buộc đăng nhập thành công cho ${processedCount} phim!`; } else if (action === 'free-login') { message = `Đã mở khóa tự do xem phim không cần đăng nhập cho ${processedCount} phim!`; } return new Response( JSON.stringify({ message, processedCount }), { status: 200, headers: { 'Content-Type': 'application/json' } } ); } catch (error) { return new Response( JSON.stringify({ error: 'Gặp lỗi khi thực hiện bulk action: ' + error.message }), { status: 500, headers: { 'Content-Type': 'application/json' } } ); } }