import {
  UserTagEntity,
  UserTagAssignmentEntity,
  TagCategoryEntity,
  UserTagLogEntity,
  TagType,
  TagAction,
} from '../entities/user-tag.entity';

export const USER_TAG_REPOSITORY = Symbol('USER_TAG_REPOSITORY');

/**
 * 用户标签仓储接口
 */
export interface UserTagRepository {
  // =====================
  // 标签分类 CRUD
  // =====================

  saveCategory(category: TagCategoryEntity): Promise<TagCategoryEntity>;
  findCategoryById(id: string): Promise<TagCategoryEntity | null>;
  findCategoryByCode(code: string): Promise<TagCategoryEntity | null>;
  findAllCategories(params?: { isEnabled?: boolean }): Promise<TagCategoryEntity[]>;
  deleteCategory(id: string): Promise<void>;

  // =====================
  // 标签定义 CRUD
  // =====================

  saveTag(tag: UserTagEntity): Promise<UserTagEntity>;
  findTagById(id: string): Promise<UserTagEntity | null>;
  findTagByCode(code: string): Promise<UserTagEntity | null>;
  findAllTags(params?: {
    categoryId?: string;
    type?: TagType;
    isEnabled?: boolean;
    isAdvertisable?: boolean;
    limit?: number;
    offset?: number;
  }): Promise<UserTagEntity[]>;
  findEnabledTags(): Promise<UserTagEntity[]>;
  findAutoTags(): Promise<UserTagEntity[]>;
  findAdvertisableTags(): Promise<UserTagEntity[]>;
  countTags(params?: {
    categoryId?: string;
    type?: TagType;
    isEnabled?: boolean;
  }): Promise<number>;
  deleteTag(id: string): Promise<void>;
  updateEstimatedUsers(tagId: string, count: number): Promise<void>;

  // =====================
  // 用户-标签关联
  // =====================

  saveAssignment(assignment: UserTagAssignmentEntity): Promise<UserTagAssignmentEntity>;
  saveAssignments(assignments: UserTagAssignmentEntity[]): Promise<void>;
  findUserTags(accountSequence: string): Promise<UserTagWithAssignment[]>;
  findUserTagIds(accountSequence: string): Promise<string[]>;
  findUsersByTagId(tagId: string, params?: {
    value?: string;
    limit?: number;
    offset?: number;
  }): Promise<UserTagAssignmentEntity[]>;
  findUsersByTagCode(tagCode: string, params?: {
    value?: string;
    limit?: number;
    offset?: number;
  }): Promise<UserTagAssignmentEntity[]>;
  countUsersByTagId(tagId: string, value?: string): Promise<number>;
  hasTag(accountSequence: string, tagId: string): Promise<boolean>;
  hasAnyTag(accountSequence: string, tagIds: string[]): Promise<boolean>;
  hasAllTags(accountSequence: string, tagIds: string[]): Promise<boolean>;
  getAssignment(accountSequence: string, tagId: string): Promise<UserTagAssignmentEntity | null>;
  deleteAssignment(accountSequence: string, tagId: string): Promise<void>;
  deleteAutoAssignments(accountSequence: string): Promise<void>;
  deleteAssignmentsByTagId(tagId: string): Promise<void>;
  deleteAssignmentsBySource(source: string): Promise<number>;
  cleanExpiredAssignments(): Promise<number>;

  // =====================
  // 标签变更日志
  // =====================

  saveLog(log: UserTagLogEntity): Promise<void>;
  saveLogs(logs: UserTagLogEntity[]): Promise<void>;
  findLogs(params: {
    accountSequence?: string;
    tagCode?: string;
    action?: TagAction;
    startDate?: Date;
    endDate?: Date;
    limit?: number;
    offset?: number;
  }): Promise<UserTagLogEntity[]>;
  countLogs(params: {
    accountSequence?: string;
    tagCode?: string;
    action?: TagAction;
    startDate?: Date;
    endDate?: Date;
  }): Promise<number>;
}

/**
 * 用户标签（包含关联信息）
 */
export interface UserTagWithAssignment {
  tag: UserTagEntity;
  assignment: UserTagAssignmentEntity;
}
