from flask import Blueprint
from flask import request
from flask import jsonify
from be.model import seller
import json

bp_seller = Blueprint("seller", __name__, url_prefix="/seller")


@bp_seller.route("/create_store", methods=["POST"])
def seller_create_store():
    user_id: str = request.json.get("user_id", "")
    store_id: str = request.json.get("store_id", "")
    se = seller.Seller()
    code, message = se.create_store(user_id=user_id, store_id=store_id)
    return jsonify({"message": message}), code


@bp_seller.route("/add_book", methods=["POST"])
def seller_add_book():
    user_id: str = request.json.get("user_id")
    store_id: str = request.json.get("store_id")
    book_info: str = request.json.get("book_info")
    stock_level: str = request.json.get("stock_level", 0)
    se = seller.Seller()
    code, message = se.add_book(user_id, store_id, book_info.get("id"), json.dumps(book_info),
                                int(stock_level))
    return jsonify({"message": message}), code


@bp_seller.route("/add_stock_level", methods=["POST"])
def add_stock_level():
    user_id: str = request.json.get("user_id")
    store_id: str = request.json.get("store_id")
    book_id: str = request.json.get("book_id")
    add_num: str = request.json.get("add_stock_level", 0)
    se = seller.Seller()
    code, message = se.add_stock_level(user_id, store_id, book_id, int(add_num))
    return jsonify({"message": message}), code
