<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Config;
use App\Models\Movie;
use App\Models\MovieLanguage;
use App\Models\TvShow;
use App\Repositories\Interfaces\Admin\ImporterInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class ImportController extends Controller
{
    protected $importer;

    public function __construct(ImporterInterface $importer)
    {
        $this->importer = $importer;
    }

    public function index()
    {
        $page_data['movie_tmdb_ids'] = Movie::pluck('tmdb_id');
        $page_data['tv_show_tmdb_ids'] = TvShow::pluck('tmdb_id')->toArray();
        $page_data['movie_languages'] = MovieLanguage::get();
        $page_data['tmdb_genres'] = $this->importer->getTmdbGenres();
        return view('admin.importer.index', $page_data);
    }

    public function fetchMovies(Request $request)
    {
        try {
            $response = $this->importer->getTMDBData($request->all());
            return response()->json($response);
        } catch (\Exception $e) {
            return response()->json([
                'results' => [],
                'total_pages' => 0,
                'error' => $e->getMessage() ?: __('error_fetching_from_tmdb'),
            ]);
        }
    }

    public function importDb($language, $type, $id)
    {
        if (isDemoMode()) {
            return response()->json([
                'status'  => 'error',
                'message' => __('This action is disabled in demo mode.'),
                'title'   => __('Error'),
                'is_reload' => false,
            ]);
        }
        if (!$language || !$type || !$id) {
            return response()->json([
                'message' => __('Something went wrong. Please try again.'),
                'title'   => __('Error'),
                'status'  => 'error',
            ]);
        }

        try {
            if ($this->importer->importFromTMDB($language, $type, $id)) {
                return response()->json([
                    'message' => __('imported_successfully'),
                    'title'   => __('Success'),
                    'status'  => 'success',
                ]);
            }
        } catch (\Exception $e) {
            $message = $e->getMessage();
            if (str_contains($message, 'Already imported') || str_contains($message, __('already_imported'))) {
                return response()->json([
                    'message' => __('this_item_already_imported'),
                    'title'   => __('Info'),
                    'status'  => 'info',
                ]);
            }
            return response()->json([
                'message' => $message ?: __('import_failed_try_again'),
                'title'   => __('Error'),
                'status'  => 'error',
            ]);
        }

        return response()->json([
            'message' => __('could_not_load_from_tmdb'),
            'title'   => __('Error'),
            'status'  => 'error',
        ]);
    }
}
