package social

import (
	"context"
	"fmt"
	"time"

	"github.com/google/uuid"
)

// AdminRelation is one entry in the admin user card's blocks / blocked-by / friends lists:
// the other account, its display name, and when the relationship was recorded. It is the
// unfiltered truth — the asymmetric block suppression that hides relationships from players
// never applies to the operator console.
type AdminRelation struct {
	AccountID   uuid.UUID
	DisplayName string
	At          time.Time
}

// AdminBlocksBy returns the accounts blockerID has blocked, with display names and block
// times, newest first — the admin card's "blocks" list.
func (svc *Service) AdminBlocksBy(ctx context.Context, blockerID uuid.UUID) ([]AdminRelation, error) {
	return svc.store.adminBlocks(ctx, blockerID, true)
}

// AdminBlockedBy returns the accounts that have blocked blockedID, with names and times,
// newest first — the admin card's "blocked by" list.
func (svc *Service) AdminBlockedBy(ctx context.Context, blockedID uuid.UUID) ([]AdminRelation, error) {
	return svc.store.adminBlocks(ctx, blockedID, false)
}

// AdminFriends returns accountID's accepted friendships in either direction, with the other
// account's name and when the friendship was accepted, newest first — the admin card's
// "friends" list.
func (svc *Service) AdminFriends(ctx context.Context, accountID uuid.UUID) ([]AdminRelation, error) {
	const q = `SELECT other_id, a.display_name, at FROM (
  SELECT CASE WHEN f.requester_id = $1 THEN f.addressee_id ELSE f.requester_id END AS other_id,
         COALESCE(f.responded_at, f.created_at) AS at
  FROM backend.friendships f
  WHERE f.status = 'accepted' AND (f.requester_id = $1 OR f.addressee_id = $1)
) rel
JOIN backend.accounts a ON a.account_id = rel.other_id
ORDER BY at DESC`
	return svc.store.adminRelations(ctx, q, accountID)
}

// adminBlocks reads the blocks touching id: when asBlocker the rows where id is the blocker
// (the other side is who they blocked), otherwise the rows where id is the blocked (the
// other side is who blocked them).
func (s *Store) adminBlocks(ctx context.Context, id uuid.UUID, asBlocker bool) ([]AdminRelation, error) {
	q := `SELECT b.blocked_id, a.display_name, b.created_at
FROM backend.blocks b JOIN backend.accounts a ON a.account_id = b.blocked_id
WHERE b.blocker_id = $1 ORDER BY b.created_at DESC`
	if !asBlocker {
		q = `SELECT b.blocker_id, a.display_name, b.created_at
FROM backend.blocks b JOIN backend.accounts a ON a.account_id = b.blocker_id
WHERE b.blocked_id = $1 ORDER BY b.created_at DESC`
	}
	return s.adminRelations(ctx, q, id)
}

// adminRelations runs an admin list query of the (account_id, display_name, at) shape.
func (s *Store) adminRelations(ctx context.Context, query string, id uuid.UUID) ([]AdminRelation, error) {
	rows, err := s.db.QueryContext(ctx, query, id)
	if err != nil {
		return nil, fmt.Errorf("social: admin relations: %w", err)
	}
	defer rows.Close()
	var out []AdminRelation
	for rows.Next() {
		var r AdminRelation
		if err := rows.Scan(&r.AccountID, &r.DisplayName, &r.At); err != nil {
			return nil, fmt.Errorf("social: scan admin relation: %w", err)
		}
		out = append(out, r)
	}
	return out, rows.Err()
}
