from flask import Blueprint, render_template, redirect, url_for, flash, request, abort
from flask_login import login_required, current_user

from app.extensions import db
from app.models.customer import Customer
from app.models.call import Call
from app.services.permissions import require
from app.services.activity_logger import log_action
from app.services import customer_service, tag_service, photo_storage
from app.blueprints.calls.forms import NEXT_ACTION_FA
from config import CUSTOMER_RATINGS

bp = Blueprint("customer_profile", __name__, url_prefix="/customers")

GENDER_FA = {"male": "آقا", "female": "خانم"}
STATUS_FA = dict(customer_service.STATUS_CHOICES)
RATING_ORDER = {"A+": 5, "A": 4, "B": 3, "C": 2, "D": 1}


@bp.route("/<int:customer_id>")
@login_required
@require("customer_profile")
def show(customer_id):
    customer = customer_service.get_customer_with_stats(customer_id)
    if not customer or customer.is_deleted:
        flash("مشتری یافت نشد", "error")
        return redirect(url_for("dashboard.index"))

    referrer = None
    if customer.referrer_customer_id:
        referrer = db.session.get(Customer, customer.referrer_customer_id)

    tags_grouped = tag_service.get_customer_tags_grouped(customer_id)
    calls = Call.query.filter_by(customer_id=customer_id).order_by(Call.call_date.desc()).all()

    log_action(current_user, "view", "customer", customer_id)

    return render_template(
        "customer_profile/show.html",
        customer=customer, referrer=referrer, tags_grouped=tags_grouped, calls=calls,
        gender_fa=GENDER_FA, status_fa=STATUS_FA, ratings=CUSTOMER_RATINGS,
        has_photo=photo_storage.photo_exists(customer.photo_path),
        all_tag_groups=tag_service.all_tags_grouped(),
        tag_service_current_ids=tag_service.get_customer_tag_ids(customer_id),
        next_action_fa=NEXT_ACTION_FA,
    )


@bp.route("/<int:customer_id>/rating", methods=["POST"])
@login_required
@require("customer_profile")
def set_rating(customer_id):
    customer = db.session.get(Customer, customer_id) or abort(404)
    rating = request.form.get("rating")
    if rating in CUSTOMER_RATINGS:
        customer.rating = rating
        customer.rating_is_manual = True
        db.session.commit()
        log_action(current_user, "update", "customer_rating", customer_id, {"rating": rating})
        flash(f"رتبه مشتری به {rating} تغییر کرد", "success")
    return redirect(url_for("customer_profile.show", customer_id=customer_id))


@bp.route("/<int:customer_id>/tags", methods=["POST"])
@login_required
@require("customer_profile")
def set_tags(customer_id):
    customer = db.session.get(Customer, customer_id) or abort(404)
    tag_ids = [int(v) for v in request.form.getlist("tag_ids")]
    tag_service.set_customer_tags(customer_id, tag_ids, assigned_by=current_user.id)
    log_action(current_user, "update", "customer_tags", customer_id)
    flash("تگ‌های مشتری به‌روزرسانی شد", "success")
    return redirect(url_for("customer_profile.show", customer_id=customer_id))


@bp.route("/<int:customer_id>/photo")
@login_required
@require("customer_profile")
def photo(customer_id):
    customer = db.session.get(Customer, customer_id) or abort(404)
    if not customer.photo_path:
        abort(404)
    return photo_storage.serve_photo(customer.photo_path)


@bp.route("/<int:customer_id>/print")
@login_required
@require("customer_profile")
def print_view(customer_id):
    customer = customer_service.get_customer_with_stats(customer_id)
    if not customer:
        abort(404)
    log_action(current_user, "print", "customer", customer_id)
    return render_template("customer_profile/print.html", customer=customer)
