#!/usr/bin/env python3
# =============================================================================
# @file    load-mock-data
# @brief   Load some sample data for DIBS
# @created 2021-01-07
# @license Please see the file named LICENSE in the project directory
# @website https://github.com/caltechlibrary/dibs
# =============================================================================

from   os.path import dirname, join, abspath
from   rich import print
from   rich.panel import Panel
import sys

sys.path.append(join(dirname(abspath(__file__)), '..'))

from dibs.data_models import database, Item, Person  # noqa: E402
from dibs.settings import config, resolved_path      # noqa: E402

# Loading dibs.data_models should create a database if it doesn't exist yet.
# Check that the file exists.
db_file = config('DATABASE_FILE', default = 'data/dibs.db')
db_path = resolved_path(db_file)
if not db_path:
    text = f'Cannot find DIBS database {db_file or "-- not configured"}'
    print(Panel(text, style = 'red', width = len(text) + 4))
    sys.exit(1)

# Connect to the database.
database.connect()

# This next one is a demo item.  We provide a manifest in ./manifests/ and
# it references an open copy of the book at the Wellcome Collection in the UK.
if not Item.get_or_none(Item.barcode == '350470000180226'):
    Item.create(barcode    = '350470000180226',
                item_id    = '2b2e4a55-7d82-4a97-9ed3-62e48b10699c',
                item_page  = 'https://search.ebscohost.com/login.aspx?direct=true&site=eds-live&scope=site&authtype=ip,guest&profid=eds&custid=s8984125&groupid=main&db=cat08655a&AN=clc.2b2e4a55.7d82.4a97.9ed3.62e48b10699c',
                title      = 'Bibliographical history of electricity & magnetism, chronologically arranged',
                author     = 'Mottelay, Paul Fleury',
                year       = '1922',
                publisher  = 'C. Griffin & company limited',
                edition    = '',
                num_copies = 1,
                duration   = 3,
                notes      = ('This is just a random book chosen because it was'
                              ' a title we could find in both an external'
                              ' public IIIF server and our Caltech catalog.'),
                ready      = True)

if not Person.select().exists():
    text = 'Now use admin/people-manager to add library staff users'
    print(Panel(text, style = 'dark_orange', width = len(text) + 4))

if not database.is_closed():
    database.close()
