package dispute

import (
	"context"

	"hfb_sys/backend/internal/model"

	"gorm.io/gorm"
)

func (r *Repository) ListForUser(ctx context.Context, userID uint64, page, pageSize int) (*PaginatedResult, error) {
	db := r.db.WithContext(ctx)
	conditions := db.Model(&model.Dispute{}).Where("initiator_id = ? OR target_user_id = ?", userID, userID)
	var total int64
	if err := conditions.Count(&total).Error; err != nil {
		return nil, err
	}
	offset := (page - 1) * pageSize
	var rows []disputeRow
	err := r.baseQuery(ctx).
		Where("d.initiator_id = ? OR d.target_user_id = ?", userID, userID).
		Order("d.id DESC").
		Offset(offset).Limit(pageSize).
		Scan(&rows).Error
	if err != nil {
		return nil, err
	}
	return &PaginatedResult{Items: toDTOs(rows), Total: total, Page: page, PageSize: pageSize}, nil
}

func (r *Repository) FindForUser(ctx context.Context, userID uint64, id uint64) (*DisputeDTO, error) {
	var row disputeRow
	if err := r.baseQuery(ctx).
		Where("d.id = ? AND (d.initiator_id = ? OR d.target_user_id = ?)", id, userID, userID).
		First(&row).Error; err != nil {
		return nil, err
	}
	dto := row.toDTO()
	return &dto, nil
}

func (r *Repository) ListAdmin(ctx context.Context, page, pageSize int) (*PaginatedResult, error) {
	var total int64
	if err := r.db.WithContext(ctx).Model(&model.Dispute{}).Count(&total).Error; err != nil {
		return nil, err
	}
	offset := (page - 1) * pageSize
	var rows []disputeRow
	err := r.baseQuery(ctx).Order("d.id DESC").Offset(offset).Limit(pageSize).Scan(&rows).Error
	if err != nil {
		return nil, err
	}
	return &PaginatedResult{Items: toDTOs(rows), Total: total, Page: page, PageSize: pageSize}, nil
}

func (r *Repository) baseQuery(ctx context.Context) *gorm.DB {
	return r.db.WithContext(ctx).Table("disputes AS d").
		Select(`d.*, o.order_no, o.status AS order_status, o.handoff_status, o.settlement_status,
			o.owner_id, o.renter_id, owner.phone AS owner_phone, renter.phone AS renter_phone,
			l.listing_no, a.title`).
		Joins("JOIN rental_orders AS o ON o.id = d.order_id").
		Joins("JOIN rental_listings AS l ON l.id = o.listing_id").
		Joins("JOIN game_accounts AS a ON a.id = o.account_id").
		Joins("JOIN users AS owner ON owner.id = o.owner_id").
		Joins("JOIN users AS renter ON renter.id = o.renter_id")
}
