from sqlalchemy.orm import Session
from app.models.role import Role, Permission, RolePermission
from .registry import register

BASE_ROLES = ["super_admin","accountant","manager","cashier","pos_user"]
PERMISSIONS = [
    # POS Module
    {"name": "pos.record_sale", "module": "pos", "action": "record", "resource": "sale"},
    {"name": "pos.reconcile", "module": "pos", "action": "reconcile", "resource": "all"},
    {"name": "pos.void_sale", "module": "pos", "action": "void", "resource": "sale"},
    {"name": "pos.refund", "module": "pos", "action": "refund", "resource": "sale"},
    # Reports Module
    {"name": "reports.view", "module": "reports", "action": "view", "resource": "all"},
    {"name": "reports.financial.view", "module": "reports", "action": "view", "resource": "financial"},
    {"name": "reports.performance.view", "module": "reports", "action": "view", "resource": "performance"},
    {"name": "reports.debtors_aging.view", "module": "reports", "action": "view", "resource": "debtors_aging"},
    {"name": "reports.export", "module": "reports", "action": "export", "resource": "all"},
    # Settings Module
    {"name": "settings.view", "module": "settings", "action": "view", "resource": "all"},
    {"name": "settings.edit", "module": "settings", "action": "edit", "resource": "all"},
    # Users Module
    {"name": "users.view", "module": "users", "action": "view", "resource": "all"},
    {"name": "users.create", "module": "users", "action": "create", "resource": "all"},
    {"name": "users.edit", "module": "users", "action": "edit", "resource": "all"},
    {"name": "users.delete", "module": "users", "action": "delete", "resource": "all"},
    # Inventory Module
    {"name": "inventory.view", "module": "inventory", "action": "view", "resource": "all"},
    {"name": "inventory.create", "module": "inventory", "action": "create", "resource": "all"},
    {"name": "inventory.edit", "module": "inventory", "action": "edit", "resource": "all"},
    {"name": "inventory.delete", "module": "inventory", "action": "delete", "resource": "all"},
    {"name": "inventory.adjust", "module": "inventory", "action": "adjust", "resource": "all"},
    # Sales Module
    {"name": "sales.view", "module": "sales", "action": "view", "resource": "all"},
    {"name": "sales.create", "module": "sales", "action": "create", "resource": "all"},
    {"name": "sales.edit", "module": "sales", "action": "edit", "resource": "all"},
    {"name": "sales.delete", "module": "sales", "action": "delete", "resource": "all"},
    {"name": "sales.approve", "module": "sales", "action": "approve", "resource": "all"},
    # Purchases Module
    {"name": "purchases.view", "module": "purchases", "action": "view", "resource": "all"},
    {"name": "purchases.create", "module": "purchases", "action": "create", "resource": "all"},
    {"name": "purchases.edit", "module": "purchases", "action": "edit", "resource": "all"},
    {"name": "purchases.delete", "module": "purchases", "action": "delete", "resource": "all"},
    {"name": "purchases.approve", "module": "purchases", "action": "approve", "resource": "all"},
    # Accounting Module
    {"name": "accounting.view", "module": "accounting", "action": "view", "resource": "all"},
    {"name": "accounting.create", "module": "accounting", "action": "create", "resource": "all"},
    {"name": "accounting.edit", "module": "accounting", "action": "edit", "resource": "all"},
    {"name": "accounting.delete", "module": "accounting", "action": "delete", "resource": "all"},
    {"name": "accounting.post", "module": "accounting", "action": "post", "resource": "all"},
    {"name": "accounting.reverse", "module": "accounting", "action": "reverse", "resource": "all"},
    # Banking Module
    {"name": "banking.view", "module": "banking", "action": "view", "resource": "all"},
    {"name": "banking.create", "module": "banking", "action": "create", "resource": "all"},
    {"name": "banking.edit", "module": "banking", "action": "edit", "resource": "all"},
    {"name": "banking.reconcile", "module": "banking", "action": "reconcile", "resource": "all"},
    # VAT Module
    {"name": "vat.view", "module": "vat", "action": "view", "resource": "all"},
    {"name": "vat.submit", "module": "vat", "action": "submit", "resource": "all"},
    {"name": "vat.edit", "module": "vat", "action": "edit", "resource": "all"},
    # Manufacturing Module
    {"name": "manufacturing.view", "module": "manufacturing", "action": "view", "resource": "all"},
    {"name": "manufacturing.create", "module": "manufacturing", "action": "create", "resource": "all"},
    {"name": "manufacturing.edit", "module": "manufacturing", "action": "edit", "resource": "all"},
    {"name": "manufacturing.approve", "module": "manufacturing", "action": "approve", "resource": "all"},
    # Workflows Module
    {"name": "workflows.view", "module": "workflows", "action": "view", "resource": "all"},
    {"name": "workflows.create", "module": "workflows", "action": "create", "resource": "all"},
    {"name": "workflows.edit", "module": "workflows", "action": "edit", "resource": "all"},
    {"name": "workflows.approve", "module": "workflows", "action": "approve", "resource": "all"},
    {"name": "workflows.admin", "module": "workflows", "action": "admin", "resource": "all"},
    # Audit Module
    {"name": "audit.view", "module": "audit", "action": "view", "resource": "all"},
    # Admin Module
    {"name": "admin.all", "module": "admin", "action": "all", "resource": "all"},
]

@register("roles_permissions")
def seed_roles_permissions(db: Session):
    existing_roles = {r.name for r in db.query(Role).all()}
    for rname in BASE_ROLES:
        if rname not in existing_roles:
            db.add(Role(name=rname, is_system_role=(rname in ["super_admin", "accountant"])))
    db.flush()

    existing_perms = {p.name: p for p in db.query(Permission).all()}
    for perm_data in PERMISSIONS:
        pname = perm_data["name"]
        if pname not in existing_perms:
            perm = Permission(
                name=perm_data["name"],
                module=perm_data["module"],
                action=perm_data["action"],
                resource=perm_data["resource"]
            )
            db.add(perm)
            db.flush()
            existing_perms[pname] = perm

    # Full access for super_admin and accountant
    all_permissions = [p["name"] for p in PERMISSIONS]

    grant_map = {
        "super_admin": all_permissions,  # Full access to everything
        "accountant": all_permissions,   # Full access to everything (same as super_admin)
        "manager": [p["name"] for p in PERMISSIONS if p["module"] in ("reports", "inventory", "sales", "purchases", "manufacturing", "workflows")],
        "cashier": [p["name"] for p in PERMISSIONS if p["module"] in ("pos", "sales")],
        "pos_user": [p["name"] for p in PERMISSIONS if p["module"] == "pos"]
    }
    roles = {r.name: r for r in db.query(Role).all()}
    for role_name, plist in grant_map.items():
        role = roles.get(role_name)
        if not role:
            continue
        for pname in plist:
            perm = existing_perms.get(pname)
            if not perm:
                continue
            link = db.query(RolePermission).filter_by(role_id=role.id, permission_id=perm.id).first()
            if not link:
                db.add(RolePermission(role_id=role.id, permission_id=perm.id))
    db.commit()
