<?php

namespace App\Http\Controllers;

use App\Models\Book;
use App\Services\BookInformation\BookLookupService;
use Cache;

class LookupController {
    public function lookup(BookLookupService $service, $isbn) {
        $result = $service->lookup($isbn);

        $arr = [
            'success' => false
        ];

        if ($result) {
            $arr = array_merge($arr, [
                'success' => true,
                'data' => $result
            ]);
        }

        $book = Book::where('barcode', $isbn)->first();

        if ($book) {
            $arr['warning'] = 'Item already exists.<br />Location: ' . $book->location->name;
        }

        return response()->json($arr);
    }
}