// models/purchaseRequestModel.js
const { getDb } = require('../dbPool');

const PurchaseRequestModel = {
  // 구매신청 목록 (소모품 정보 LEFT JOIN — item_id NULL 허용, batch 정보 포함)
  async getAll(filters = {}) {
    const db = await getDb();
    let sql = `
      SELECT pr.*, ci.item_name, ci.spec, ci.maker, ci.category, ci.base_price, ci.unit,
             ci.photo_path AS ci_photo_path, pr.photo_path AS pr_photo_path,
             pr.custom_item_name, pr.custom_category,
             su.name AS requester_name,
             pb.batch_name, pb.status AS batch_status, pb.category AS batch_category
      FROM purchase_requests pr
      LEFT JOIN consumable_items ci ON pr.item_id = ci.item_id
      LEFT JOIN sso_users su ON pr.requester_id = su.user_id
      LEFT JOIN purchase_batches pb ON pr.batch_id = pb.batch_id
      WHERE 1=1
    `;
    const params = [];

    if (filters.status) { sql += ' AND pr.status = ?'; params.push(filters.status); }
    if (filters.requester_id) { sql += ' AND pr.requester_id = ?'; params.push(filters.requester_id); }
    if (filters.category) {
      sql += ' AND (ci.category = ? OR pr.custom_category = ?)';
      params.push(filters.category, filters.category);
    }
    if (filters.from_date) { sql += ' AND pr.request_date >= ?'; params.push(filters.from_date); }
    if (filters.to_date) { sql += ' AND pr.request_date <= ?'; params.push(filters.to_date); }
    if (filters.batch_id) { sql += ' AND pr.batch_id = ?'; params.push(filters.batch_id); }

    sql += ' ORDER BY pr.created_at DESC';
    const [rows] = await db.query(sql, params);
    return rows;
  },

  // 단건 조회 (batch 정보 포함)
  async getById(requestId) {
    const db = await getDb();
    const [rows] = await db.query(`
      SELECT pr.*, ci.item_name, ci.spec, ci.maker, ci.category, ci.base_price, ci.unit,
             ci.photo_path AS ci_photo_path, pr.photo_path AS pr_photo_path,
             pr.custom_item_name, pr.custom_category,
             su.name AS requester_name,
             pb.batch_name, pb.status AS batch_status, pb.category AS batch_category,
             rsu.name AS received_by_name
      FROM purchase_requests pr
      LEFT JOIN consumable_items ci ON pr.item_id = ci.item_id
      LEFT JOIN sso_users su ON pr.requester_id = su.user_id
      LEFT JOIN purchase_batches pb ON pr.batch_id = pb.batch_id
      LEFT JOIN sso_users rsu ON pr.received_by = rsu.user_id
      WHERE pr.request_id = ?
    `, [requestId]);
    return rows[0] || null;
  },

  // 구매신청 생성
  async create(data) {
    const db = await getDb();
    const [result] = await db.query(
      `INSERT INTO purchase_requests (item_id, custom_item_name, custom_category, quantity, requester_id, request_date, notes, photo_path)
       VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
      [data.item_id || null, data.custom_item_name || null, data.custom_category || null,
       data.quantity || 1, data.requester_id, data.request_date, data.notes || null, data.photo_path || null]
    );
    return this.getById(result.insertId);
  },

  // 상태 변경 (보류)
  async hold(requestId, holdReason) {
    const db = await getDb();
    await db.query(
      `UPDATE purchase_requests SET status = 'hold', hold_reason = ? WHERE request_id = ?`,
      [holdReason || null, requestId]
    );
    return this.getById(requestId);
  },

  // 상태 → purchased
  async markPurchased(requestId) {
    const db = await getDb();
    await db.query(
      `UPDATE purchase_requests SET status = 'purchased' WHERE request_id = ?`,
      [requestId]
    );
  },

  // pending으로 되돌리기
  async revertToPending(requestId) {
    const db = await getDb();
    await db.query(
      `UPDATE purchase_requests SET status = 'pending', hold_reason = NULL WHERE request_id = ?`,
      [requestId]
    );
    return this.getById(requestId);
  },

  // item_id 업데이트 (마스터 등록 후)
  async updateItemId(requestId, itemId) {
    const db = await getDb();
    await db.query(
      `UPDATE purchase_requests SET item_id = ? WHERE request_id = ?`,
      [itemId, requestId]
    );
  },

  // 삭제 (admin only, pending 상태만)
  async delete(requestId) {
    const db = await getDb();
    const [result] = await db.query(
      `DELETE FROM purchase_requests WHERE request_id = ? AND status = 'pending'`,
      [requestId]
    );
    return result.affectedRows > 0;
  },

  // 내 신청 목록 (모바일용, 페이지네이션)
  async getMyRequests(userId, { page = 1, limit = 20, status } = {}) {
    const db = await getDb();
    const offset = (page - 1) * limit;
    let where = 'WHERE pr.requester_id = ?';
    const params = [userId];

    if (status) { where += ' AND pr.status = ?'; params.push(status); }

    const [[{ total }]] = await db.query(
      `SELECT COUNT(*) AS total FROM purchase_requests pr ${where}`, params
    );

    const [rows] = await db.query(`
      SELECT pr.*, ci.item_name, ci.spec, ci.maker, ci.category, ci.base_price, ci.unit,
             ci.photo_path AS ci_photo_path, pr.photo_path AS pr_photo_path,
             pr.custom_item_name, pr.custom_category,
             pb.batch_name, pb.status AS batch_status,
             rsu.name AS received_by_name
      FROM purchase_requests pr
      LEFT JOIN consumable_items ci ON pr.item_id = ci.item_id
      LEFT JOIN purchase_batches pb ON pr.batch_id = pb.batch_id
      LEFT JOIN sso_users rsu ON pr.received_by = rsu.user_id
      ${where}
      ORDER BY pr.created_at DESC
      LIMIT ? OFFSET ?
    `, [...params, limit, offset]);

    return {
      data: rows,
      pagination: { page, limit, total, totalPages: Math.ceil(total / limit) }
    };
  },

  // batch에 요청 그룹화 (status → grouped)
  async groupIntoBatch(requestIds, batchId) {
    const db = await getDb();
    await db.query(
      `UPDATE purchase_requests SET batch_id = ?, status = 'grouped'
       WHERE request_id IN (?) AND status = 'pending' AND batch_id IS NULL`,
      [batchId, requestIds]
    );
  },

  // batch에서 제거 (status → pending 복원)
  async removeFromBatch(requestIds) {
    const db = await getDb();
    await db.query(
      `UPDATE purchase_requests SET batch_id = NULL, status = 'pending'
       WHERE request_id IN (?) AND status = 'grouped'`,
      [requestIds]
    );
  },

  // batch 내 전체 요청 purchased 전환
  async markBatchPurchased(batchId) {
    const db = await getDb();
    await db.query(
      `UPDATE purchase_requests SET status = 'purchased' WHERE batch_id = ? AND status = 'grouped'`,
      [batchId]
    );
  },

  // 개별 입고 처리
  async receive(requestId, { receivedPhotoPath, receivedLocation, receivedBy }) {
    const db = await getDb();
    await db.query(
      `UPDATE purchase_requests
       SET status = 'received', received_photo_path = ?, received_location = ?,
           received_at = NOW(), received_by = ?
       WHERE request_id = ? AND status = 'purchased'`,
      [receivedPhotoPath || null, receivedLocation || null, receivedBy, requestId]
    );
    return this.getById(requestId);
  },

  // batch 내 전체 입고 처리
  async receiveBatch(batchId, { receivedPhotoPath, receivedLocation, receivedBy }) {
    const db = await getDb();
    await db.query(
      `UPDATE purchase_requests
       SET status = 'received', received_photo_path = ?, received_location = ?,
           received_at = NOW(), received_by = ?
       WHERE batch_id = ? AND status = 'purchased'`,
      [receivedPhotoPath || null, receivedLocation || null, receivedBy, batchId]
    );
  },

  // batch 내 모든 요청이 received인지 확인
  async checkBatchAllReceived(batchId) {
    const db = await getDb();
    const [[{ total, received }]] = await db.query(
      `SELECT COUNT(*) AS total,
              SUM(CASE WHEN status = 'received' THEN 1 ELSE 0 END) AS received
       FROM purchase_requests WHERE batch_id = ?`,
      [batchId]
    );
    return total > 0 && total === received;
  },

  // grouped 상태에서 hold (batch에서 자동 제거)
  async holdFromGrouped(requestId, holdReason) {
    const db = await getDb();
    await db.query(
      `UPDATE purchase_requests SET status = 'hold', hold_reason = ?, batch_id = NULL
       WHERE request_id = ? AND status = 'grouped'`,
      [holdReason || null, requestId]
    );
    return this.getById(requestId);
  },

  // batch 내 신청자 ID 목록 조회
  async getRequesterIdsByBatch(batchId) {
    const db = await getDb();
    const [rows] = await db.query(
      `SELECT DISTINCT requester_id FROM purchase_requests WHERE batch_id = ?`,
      [batchId]
    );
    return rows.map(r => r.requester_id);
  },

  // 구매 취소 (purchased → pending 복원, batch에서도 제거)
  async cancelPurchase(requestId, { cancelledBy, cancelReason }) {
    const db = await getDb();
    await db.query(
      `UPDATE purchase_requests
       SET status = 'cancelled', cancelled_at = NOW(), cancelled_by = ?, cancel_reason = ?,
           batch_id = NULL
       WHERE request_id = ? AND status = 'purchased'`,
      [cancelledBy, cancelReason || null, requestId]
    );
    return this.getById(requestId);
  },

  // 반품 (received → returned)
  async returnItem(requestId, { cancelledBy, cancelReason }) {
    const db = await getDb();
    await db.query(
      `UPDATE purchase_requests
       SET status = 'returned', cancelled_at = NOW(), cancelled_by = ?, cancel_reason = ?
       WHERE request_id = ? AND status = 'received'`,
      [cancelledBy, cancelReason || null, requestId]
    );
    return this.getById(requestId);
  },

  // 취소/반품에서 원래 상태로 되돌리기
  async revertCancel(requestId) {
    const db = await getDb();
    await db.query(
      `UPDATE purchase_requests
       SET status = 'pending', cancelled_at = NULL, cancelled_by = NULL, cancel_reason = NULL
       WHERE request_id = ? AND status = 'cancelled'`,
      [requestId]
    );
    return this.getById(requestId);
  }
};

module.exports = PurchaseRequestModel;
