// 员工端API服务
import { get, post } from '../utils/request';
import { API } from '../config/api';

// 员工信息接口
export interface EmployeeInfo {
  id: number;
  name: string;
  phone: string;
  role: string;
  enterprise_id: number;
  enterprise_name: string;
  avatar?: string;
  is_bound_xhs: number;
  xhs_account: string;
  xhs_phone: string;
  has_xhs_cookie?: boolean;  // 是否有Cookie（不返回完整Cookie内容）
  bound_at?: string;
}

// 产品接口
export interface Product {
  id: number;
  name: string;
  image: string;
  description: string;
  available_copies: number;
}

// 文案接口
export interface Copy {
  id: number;
  product_id: number;
  topic: string;
  title: string;
  content: string;
  created_at: string;
  images?: Array<{
    id: number;
    image_url: string;
    image_thumb_url: string;
    sort_order: number;
    keywords_name: string;
  }>;
  tags?: string[];
}

// 发布记录接口
export interface PublishRecord {
  id: number;
  product_id: number;
  product_name: string;
  topic: string;
  title: string;
  publish_link: string;
  publish_time: string;
  images: Array<{
    id: number;
    image_url: string;
    image_thumb_url: string;
    sort_order: number;
    keywords_name: string;
  }>;
  tags: string[];
}

/**
 * 员工API服务类
 */
export class EmployeeService {
  /**
   * 发送小红书验证码
   */
  static async sendXHSCode(xhsPhone: string) {
    return post(API.xhs.sendCode, {
      xhs_phone: xhsPhone
    });
  }

  /**
   * 获取员工个人信息
   */
  static async getProfile() {
    return get<EmployeeInfo>(API.employee.profile);
  }

  /**
   * 绑定小红书账号
   */
  static async bindXHS(xhsPhone: string, code: string) {
    return post<{ xhs_account: string }>(API.employee.bindXHS, {
      xhs_phone: xhsPhone,
      code
    });
  }

  /**
   * 解绑小红书账号
   */
  static async unbindXHS() {
    return post(API.employee.unbindXHS);
  }

  /**
   * 获取产品列表（公开接口，不需要登录）
   */
  static async getProducts() {
    return get<{ list: Product[] }>(API.public.products);
  }

  /**
   * 获取可领取文案列表
   */
  static async getAvailableCopies(productId: number) {
    return get<{
      product: {
        id: number;
        name: string;
        image: string;
      };
      copies: Copy[];
    }>(API.employee.availableCopies, { product_id: productId });
  }

  /**
   * 领取文案
   */
  static async claimCopy(copyId: number, productId: number) {
    return post<{
      claim_id: number;
      copy: {
        id: number;
        title: string;
        content: string;
        images: string[];
      };
    }>(API.employee.claimCopy, {
      copy_id: copyId,
      product_id: productId
    });
  }

  /**
   * 随机领取文案
   */
  static async claimRandomCopy(productId: number) {
    return post<{
      claim_id: number;
      copy: {
        id: number;
        title: string;
        content: string;
        images: string[];
      };
    }>(API.employee.claimRandomCopy, {
      product_id: productId
    });
  }

  /**
   * 发布内容
   */
  static async publish(params: {
    copy_id: number;
    title: string;
    content: string;
    publish_link?: string;
    xhs_note_id?: string;
  }) {
    return post<{ record_id: number }>(API.employee.publish, params);
  }

  /**
   * 获取我的发布记录
   */
  static async getMyPublishRecords(page: number = 1, pageSize: number = 10) {
    return get<{
      total: number;
      list: PublishRecord[];
    }>(API.employee.myPublishRecords, {
      page,
      page_size: pageSize
    });
  }

  /**
   * 获取发布记录详情
   */
  static async getPublishRecordDetail(recordId: number) {
    return get<{
      id: number;
      article_id: number;
      product_id: number;
      product_name: string;
      topic: string;
      title: string;
      content: string;
      images: Array<{
        id: number;
        image_url: string;
        image_thumb_url: string;
        sort_order: number;
        keywords_name: string;
      }>;
      tags: string[];
      coze_tag: string;
      publish_link: string;
      xhs_note_id: string;
      status: string;
      publish_time: string;
    }>(`/api/employee/publish-record/${recordId}`);
  }
}
