from flask import Flask, jsonify, abort
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
from sqlalchemy import UniqueConstraint
from dataclasses import dataclass
import requests
import json
from producer import publish

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:root@db/main'
CORS(app)

db = SQLAlchemy(app)

@dataclass
class Product(db.Model):
    """
        represents a product previously created in django app but manipulated here 

    """
    id: int
    title: str
    image: str


    id = db.Column(db.Integer, primary_key=True, autoincrement=False)
    title = db.Column(db.String(200))
    image = db.Column(db.String(200)) 

@dataclass
class ProductUser(db.Model):
    

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer)
    product_id = db.Column(db.Integer)

    UniqueConstraint('user_id', 'product_id', name='user_product_unique')


@app.route('/api/products')
def index():
    """ 
        entry point flask application
        returns a list of products
    """    
    return jsonify(Product.query.all())

@app.route('/api/products/<int:id>/like', methods=['POST'])
def like(id):
    """
        create a unique like per produced by the current user
        which is crerated randomly in this case
    """

    req = requests.get('http://docker.for.linux.localhost:8000/api/user')
    
    json = req.json()

    try:
        productUser = ProductUser(user_id=json['id'], product_id=id)
        db.session.add(productUser)
        db.session.commit()

        publish('product_liked', id) 
    except:
        abort(400, 'You already liked this product')


    return jsonify({
        'mesage': 'success'
    })
    

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')
