import { hasPermission } from '@/lib/auth/authorization';
import type { Permission } from '@/lib/auth/permissions';

export type ContentModule = 'announcement' | 'online_lesson';

export type ContentApprovalStatus =
  | 'draft'
  | 'pending_review'
  | 'approved'
  | 'published'
  | 'returned'
  | 'rejected'
  | 'archived';

export type ContentApprovalAction =
  | 'submit'
  | 'approve'
  | 'return'
  | 'reject'
  | 'publish'
  | 'archive';

export const contentApprovalStatusLabels: Record<ContentApprovalStatus, string> = {
  draft: 'ฉบับร่าง',
  pending_review: 'รอตรวจสอบ',
  approved: 'อนุมัติแล้ว',
  published: 'เผยแพร่แล้ว',
  returned: 'ส่งกลับแก้ไข',
  rejected: 'ไม่อนุมัติ',
  archived: 'เก็บถาวร'
};

export const contentApprovalStatusVariants: Record<
  ContentApprovalStatus,
  'secondary' | 'default' | 'outline' | 'destructive'
> = {
  draft: 'secondary',
  pending_review: 'outline',
  approved: 'default',
  published: 'default',
  returned: 'outline',
  rejected: 'destructive',
  archived: 'outline'
};

export const contentApprovalEditableStatusOptions: Array<{
  value: ContentApprovalStatus;
  label: string;
}> = [
  { value: 'draft', label: contentApprovalStatusLabels.draft },
  { value: 'pending_review', label: contentApprovalStatusLabels.pending_review }
];

const actionPermissionMap: Record<ContentModule, Record<ContentApprovalAction, Permission>> = {
  announcement: {
    submit: 'announcement:submit',
    approve: 'announcement:approve',
    return: 'announcement:revision',
    reject: 'announcement:reject',
    publish: 'announcement:publish',
    archive: 'announcement:archive'
  },
  online_lesson: {
    submit: 'online_lesson:submit',
    approve: 'online_lesson:approve',
    return: 'online_lesson:revision',
    reject: 'online_lesson:reject',
    publish: 'online_lesson:publish',
    archive: 'online_lesson:archive'
  }
};

const editablePermissionMap: Record<ContentModule, Permission> = {
  announcement: 'announcement:edit_draft',
  online_lesson: 'online_lesson:edit_draft'
};

const deletePermissionMap: Record<ContentModule, Permission> = {
  announcement: 'announcement:delete_draft',
  online_lesson: 'online_lesson:delete_draft'
};

export function getContentActionPermission(module: ContentModule, action: ContentApprovalAction) {
  return actionPermissionMap[module][action];
}

export function getEditablePermission(module: ContentModule) {
  return editablePermissionMap[module];
}

export function getDeleteDraftPermission(module: ContentModule) {
  return deletePermissionMap[module];
}

export function canReadAllContent(module: ContentModule, permissions: readonly string[] | null | undefined) {
  return hasPermission(
    permissions,
    module === 'announcement' ? 'announcement:read_all' : 'online_lesson:read_all'
  );
}

export function canCreateContent(module: ContentModule, permissions: readonly string[] | null | undefined) {
  return hasPermission(
    permissions,
    module === 'announcement' ? 'announcement:create' : 'online_lesson:create'
  );
}

export function canEditContent({
  module,
  permissions,
  status,
  isOwner
}: {
  module: ContentModule;
  permissions: readonly string[] | null | undefined;
  status: ContentApprovalStatus;
  isOwner: boolean;
}) {
  if (status === 'draft' && isOwner) {
    return hasPermission(permissions, getEditablePermission(module));
  }

  if ((status === 'returned' || status === 'rejected') && isOwner) {
    return hasPermission(permissions, getEditablePermission(module));
  }

  if (status === 'approved' || status === 'published') {
    return hasPermission(permissions, getContentActionPermission(module, 'approve'));
  }

  return false;
}

export function canDeleteContent({
  module,
  permissions,
  status,
  isOwner
}: {
  module: ContentModule;
  permissions: readonly string[] | null | undefined;
  status: ContentApprovalStatus;
  isOwner: boolean;
}) {
  return status === 'draft' && isOwner && hasPermission(permissions, getDeleteDraftPermission(module));
}

export function canTransitionContent({
  module,
  action,
  currentStatus,
  permissions,
  isOwner
}: {
  module: ContentModule;
  action: ContentApprovalAction;
  currentStatus: ContentApprovalStatus;
  permissions: readonly string[] | null | undefined;
  isOwner: boolean;
}) {
  if (!hasPermission(permissions, getContentActionPermission(module, action))) {
    return false;
  }

  switch (action) {
    case 'submit':
      return isOwner && (currentStatus === 'draft' || currentStatus === 'returned' || currentStatus === 'rejected');
    case 'approve':
      return currentStatus === 'pending_review';
    case 'return':
      return currentStatus === 'pending_review';
    case 'reject':
      return currentStatus === 'pending_review';
    case 'publish':
      return currentStatus === 'approved';
    case 'archive':
      return currentStatus === 'published';
    default:
      return false;
  }
}

export function getNextContentStatus(action: ContentApprovalAction): ContentApprovalStatus {
  switch (action) {
    case 'submit':
      return 'pending_review';
    case 'approve':
      return 'approved';
    case 'return':
      return 'returned';
    case 'reject':
      return 'rejected';
    case 'publish':
      return 'published';
    case 'archive':
      return 'archived';
  }
}
