<gh_stars>0
from core.settings import VERSION_API, TIME_ZONE
from zoneinfo import ZoneInfo
from datetime import datetime, timezone
from core.models import Unit, Note
from flask import jsonify
from core.instance import app
from core.session import *



# GET ALL NOTES
@app.route(f'/api/{VERSION_API}/notes', methods=['GET', 'POST'])
@token_required
def get_all_notes(current_unit):
    notes = Note.query.all()
    units = Unit.query.all()
    result = []
    tz = ZoneInfo(TIME_ZONE)

    for note in notes:
        note_data = {}
        note_data['id'] = note.id
        note_data['title'] = note.title
        note_data['message'] = note.message

        dt_utc = datetime(int(note.date.year), int(note.date.month), int(note.date.day), \
                            int(note.date.hour), int(note.date.minute), tzinfo=timezone.utc)
        dt_ba = dt_utc.astimezone(tz)
        note_data['date'] = dt_ba.strftime(f"%d/%m/%Y %H:%M ARG")
        for unit in units:
            if unit.id == note.unit_id:
                note_data['unit_name'] = unit.name
                note_data['public_id'] = unit.public_id
        
        result.append(note_data)
    return jsonify({'notes': result})


# GET NOTES WITH A PUBLIC_ID
@app.route(f'/api/{VERSION_API}/notes/<public_id>', methods=['GET', 'POST'])
@token_required
def get_all_notes_id(current_unit, public_id):
    notes = Note.query.filter_by(public_id=public_id)
    units = Unit.query.all()
    count = 0
    result = []
    tz = ZoneInfo(TIME_ZONE)

    for note in notes:
        count = count + 1
        note_data = {}
        note_data['id'] = note.id
        note_data['title'] = note.title
        note_data['message'] = note.message

        dt_utc = datetime(int(note.date.year), int(note.date.month), int(note.date.day), \
                            int(note.date.hour), int(note.date.minute), tzinfo=timezone.utc)
        dt_ba = dt_utc.astimezone(tz)
        note_data['date'] = dt_ba.strftime(f"%d/%m/%Y %H:%M ARG")
        for unit in units:
            if unit.id == note.unit_id:
                note_data['unit_name'] = unit.name
                note_data['public_id'] = unit.public_id
        
        result.append(note_data)
    return jsonify({'notes': result, 'count': count})


# GET NOTES WITH A PUBLIC_ID
@app.route(f'/api/{VERSION_API}/notes/session', methods=['GET', 'POST'])
@token_required
def get_all_notes_session(current_unit):
    notes = Note.query.filter_by(public_id=current_unit.public_id)
    count = 0
    units = Unit.query.all()
    result = []
    tz = ZoneInfo(TIME_ZONE)

    for note in notes:
        count = count + 1
        note_data = {}
        note_data['id'] = note.id
        note_data['title'] = note.title
        note_data['message'] = note.message

        dt_utc = datetime(int(note.date.year), int(note.date.month), int(note.date.day), \
                            int(note.date.hour), int(note.date.minute), tzinfo=timezone.utc)
        dt_ba = dt_utc.astimezone(tz)
        note_data['date'] = dt_ba.strftime(f"%d/%m/%Y %H:%M ARG")
        for unit in units:
            if unit.id == note.unit_id:
                note_data['unit_name'] = unit.name
                note_data['public_id'] = unit.public_id
        
        result.append(note_data)
    return jsonify({'notes': result, 'count': count})


# CREATE NOTA
@app.route(f'/api/{VERSION_API}/create/note', methods=['POST'])
@token_required
def create_note(current_unit):
    data = request.get_json()
    new_note = Note(title=data['title'], message=data['message'], public_id=current_unit.public_id, unit_id=current_unit.id, unit_name=current_unit.name)
    db.session.add(new_note)
    db.session.commit()
    return make_response(jsonify({'message': 'New note created.'}), 201)


# DELETE NOTA
@app.route(f'/api/{VERSION_API}/notes/delete/<note_id>', methods=['DELETE'])
@token_required
def delete_note(current_unit, note_id):
    note = Note.query.filter_by(id=note_id, unit_id=current_unit.id).first()
    if not note:
        return jsonify({'message': 'Note does not exist.'})
    db.session.delete(note)
    db.session.commit()
    return jsonify({'message': 'Note deleted.'})


# UPDATE NOTA
@app.route(f'/api/{VERSION_API}/notes/update/<note_id>', methods=['PUT'])
@token_required
def update_note(current_unit, note_id):
    data = request.get_json()
    note = Note.query.filter_by(id=note_id, unit_id=current_unit.id).first()
    if not note:
        return jsonify({'message': 'Note does not exist.'})
    note.title = data['title']
    note.message = data['message']
    db.session.merge(note)
    db.session.flush()
    db.session.commit()
    return jsonify({'message': 'Note updated.'})
