# -*- coding: utf-8 -*-
"""
Server-side copy of the price / product facts.

WHY THIS FILE EXISTS
--------------------
The browser posts only *what the customer chose* (which books, how many, which
delivery area). It never posts money. Every taka figure stored against an order
is calculated here, on the server, so a customer cannot edit the page and order
the whole set for 1 taka.

KEEP IN SYNC
------------
The numbers below must match `const CONFIG = { ... }` inside
templates/index.html. If you change a price on the page, change it here too.

HOW A BOOK IS PRICED
--------------------
A single book's price comes from its page count, not from its name:

    6 pages -> 90 Tk      8 pages -> 120 Tk
   12 pages -> 150 Tk    28 pages -> 260 Tk

Buy every book at once and it stops being 13 separate books: it becomes THE SET,
which costs SET_PRICE and ships free.
"""

PRODUCT_NAME = '১৩টি কাপড়ের বইয়ের সেট'
PRODUCT_NAME_EN = '13 Learning Cloth Books Set'
PRODUCT_SKU = 'AARISH-CB-SET-13'

# ---------------------------------------------------------------- the books
# slug is the stable id the browser sends back. Never rename a slug that has
# already been used by a real order — the admin reads it on old orders.
BOOKS = [
    {'slug': 'pet-animal',     'bn': 'পোষা প্রাণী',  'en': 'Pet Animal',     'pages': 8},
    {'slug': 'weather-chart',  'bn': 'আবহাওয়া',     'en': 'Weather Chart',  'pages': 12},
    {'slug': 'all-colours',    'bn': 'সব রং',        'en': 'All Colours',    'pages': 6},
    {'slug': 'aquatic-animal', 'bn': 'জলচর প্রাণী',  'en': 'Aquatic Animal', 'pages': 8},
    {'slug': 'the-family',     'bn': 'পরিবার',       'en': 'The Family',     'pages': 12},
    {'slug': 'birds',          'bn': 'পাখি',         'en': 'Birds',          'pages': 8},
    {'slug': 'number',         'bn': 'সংখ্যা',       'en': 'Number',         'pages': 12},
    {'slug': 'vehicles',       'bn': 'যানবাহন',      'en': 'Vehicles',       'pages': 8},
    {'slug': 'vegetable',      'bn': 'সবজি',         'en': 'Vegetable',      'pages': 8},
    {'slug': 'alphabet',       'bn': 'বর্ণমালা',     'en': 'Alphabet',       'pages': 28},
    {'slug': 'fruits',         'bn': 'ফলমূল',        'en': 'Fruits',         'pages': 8},
    {'slug': 'feelings',       'bn': 'অনুভূতি',      'en': 'Feelings',       'pages': 12},
    {'slug': 'forest-animals', 'bn': 'বনের প্রাণী',  'en': 'Forest Animals', 'pages': 8},
]

BOOKS_BY_SLUG = {b['slug']: b for b in BOOKS}
ALL_SLUGS = [b['slug'] for b in BOOKS]

# ------------------------------------------------- price of a single book
PAGE_PRICES = {6: 90, 8: 120, 12: 150, 28: 260}
PAGE_PRICE_FALLBACK = 120       # used if a book has a page count not listed above

# ------------------------------------------------------ price of the set
SET_PRICE = 1150                # what all 13 books together cost
REGULAR_PRICE = 1850            # the crossed-out "before" price of the set
SET_FREE_DELIVERY = True        # the whole set always ships free

DELIVERY_INSIDE = 60
DELIVERY_OUTSIDE = 100
FREE_DELIVERY_OVER = 0          # 0 = the "spend X, ship free" rule is switched off

MIN_QTY = 1
MAX_QTY = 10

GIFT = 'একটি প্রিমিয়াম কিডস টি-শার্ট'      # '' = no free gift
GIFT_SIZES = ['1Y', '2Y', '3Y', '4Y', '5Y']
GIFT_WITH_FULL_SET_ONLY = True  # False = every order gets the free t-shirt


def book_price(book):
    """What one copy of a single book costs, from its page count."""
    return PAGE_PRICES.get(book['pages'], PAGE_PRICE_FALLBACK)


def clean_slugs(slugs):
    """Keep only real book slugs, no duplicates, in the catalogue's own order."""
    wanted = set(str(s).strip() for s in (slugs or []))
    return [s for s in ALL_SLUGS if s in wanted]


def is_full_set(slugs):
    return len(clean_slugs(slugs)) == len(ALL_SLUGS)


def selection_price(slugs):
    """Price for ONE copy of what the customer ticked.

    All 13 books -> the set price. Anything less -> the books added up.
    """
    chosen = clean_slugs(slugs)
    if not chosen:
        return 0
    if len(chosen) == len(ALL_SLUGS):
        return SET_PRICE
    return sum(book_price(BOOKS_BY_SLUG[s]) for s in chosen)


def delivery_charge(subtotal, area, full_set=False):
    """Delivery charge in taka. The complete set always ships free."""
    if full_set and SET_FREE_DELIVERY:
        return 0
    if FREE_DELIVERY_OVER > 0 and subtotal >= FREE_DELIVERY_OVER:
        return 0
    return DELIVERY_INSIDE if area == 'inside' else DELIVERY_OUTSIDE


def quote(slugs, quantity, area):
    """The full money breakdown for an order. Never trusts the client."""
    chosen = clean_slugs(slugs)
    full = len(chosen) == len(ALL_SLUGS) and len(chosen) > 0
    unit = selection_price(chosen)
    subtotal = unit * quantity
    delivery = delivery_charge(subtotal, area, full_set=full)
    return {
        'unit_price': unit,
        'subtotal': subtotal,
        'delivery_charge': delivery,
        'total': subtotal + delivery,
    }


def gift_applies(slugs):
    """Does this order come with the free t-shirt?"""
    if not GIFT:
        return False
    if GIFT_WITH_FULL_SET_ONLY:
        return is_full_set(slugs)
    return True


def describe(slugs):
    """A short line for the admin list: what did they actually order?"""
    chosen = clean_slugs(slugs)
    if not chosen:
        return ''
    if len(chosen) == len(ALL_SLUGS):
        return 'Full set (%d books)' % len(ALL_SLUGS)
    names = [BOOKS_BY_SLUG[s]['en'] for s in chosen]
    if len(names) <= 3:
        return '%d books: %s' % (len(names), ', '.join(names))
    return '%d books: %s + %d more' % (len(names), ', '.join(names[:3]), len(names) - 3)
