<?php

namespace Modules\Feedback\Repositories;

use Illuminate\Database\Eloquent\Collection;
use Modules\Feedback\Entities\Feedback;
use Modules\Feedback\Repositories\Contracts\FeedbackRepositoryInterface;

class FeedbackRepository implements FeedbackRepositoryInterface
{
    /**
     * Get all feedback with pagination and search
     */
    public function getAllPaginated(int $perPage = 10, string $search = '', string $sortBy = 'id', string $sortOrder = 'asc', array $filters = [])
    {
        $query = Feedback::with(['user', 'assignedUser', 'documents'])->orderBy($sortBy, $sortOrder);

        if (!empty($search)) {
            $query->where(function ($q) use ($search) {
                $q->where('title', 'ILIKE', "%{$search}%")
                  ->orWhere('description', 'ILIKE', "%{$search}%")
                  ->orWhereHas('user', function ($userQuery) use ($search) {
                      $userQuery->where('name', 'ILIKE', "%{$search}%")
                                ->orWhere('email', 'ILIKE', "%{$search}%");
                  });
            });
        }

        // Apply filters
        if (!empty($filters['type'])) {
            $query->where('type', $filters['type']);
        }

        if (!empty($filters['status'])) {
            $query->where('status', $filters['status']);
        }

        if (!empty($filters['priority'])) {
            $query->where('priority', $filters['priority']);
        }

        return $query->paginate($perPage);
    }

    /**
     * Get all feedback with their relationships and pagination with search
     */
    public function getAllWithRelationsPaginated(int $perPage = 10, string $search = '', string $sortBy = 'id', string $sortOrder = 'asc', array $filters = [])
    {
        $query = Feedback::with(['user', 'assignedUser', 'documents'])->orderBy($sortBy, $sortOrder);

        if (!empty($search)) {
            $query->where(function ($q) use ($search) {
                $q->where('title', 'ILIKE', "%{$search}%")
                  ->orWhere('description', 'ILIKE', "%{$search}%")
                  ->orWhereHas('user', function ($userQuery) use ($search) {
                      $userQuery->where('name', 'ILIKE', "%{$search}%")
                                ->orWhere('email', 'ILIKE', "%{$search}%");
                  });
            });
        }

        // Apply filters
        if (!empty($filters['type'])) {
            $query->where('type', $filters['type']);
        }

        if (!empty($filters['status'])) {
            $query->where('status', $filters['status']);
        }

        if (!empty($filters['priority'])) {
            $query->where('priority', $filters['priority']);
        }

        return $query->paginate($perPage);
    }

    /**
     * Get all feedback with their relationships and search
     */
    public function getAllWithRelations(string $search = '', string $sortBy = 'id', string $sortOrder = 'asc'): Collection
    {
        $query = Feedback::with(['user', 'assignedUser', 'documents'])->orderBy($sortBy, $sortOrder);

        if (!empty($search)) {
            $query->where(function ($q) use ($search) {
                $q->where('title', 'ILIKE', "%{$search}%")
                  ->orWhere('description', 'ILIKE', "%{$search}%")
                  ->orWhereHas('user', function ($userQuery) use ($search) {
                      $userQuery->where('name', 'ILIKE', "%{$search}%")
                                ->orWhere('email', 'ILIKE', "%{$search}%");
                  });
            });
        }

        return $query->get();
    }

    /**
     * Create a new feedback
     */
    public function create(array $data): Feedback
    {
        return Feedback::create($data);
    }

    /**
     * Find feedback by ID
     */
    public function findById(string $id): ?Feedback
    {
        return Feedback::with(['user', 'assignedUser', 'documents'])->find($id);
    }

    /**
     * Delete feedback (soft delete)
     */
    public function delete(string $id): bool
    {
        $feedback = Feedback::find($id);
        if ($feedback) {
            return $feedback->delete();
        }

        return false;
    }

    /**
     * Get all feedback
     */
    public function all(string $search = '', string $sortBy = 'id', string $sortOrder = 'asc'): Collection
    {
        $query = Feedback::with(['user', 'assignedUser', 'documents'])->orderBy($sortBy, $sortOrder);

        if (!empty($search)) {
            $query->where(function ($q) use ($search) {
                $q->where('title', 'ILIKE', "%{$search}%")
                  ->orWhere('description', 'ILIKE', "%{$search}%")
                  ->orWhereHas('user', function ($userQuery) use ($search) {
                      $userQuery->where('name', 'ILIKE', "%{$search}%")
                                ->orWhere('email', 'ILIKE', "%{$search}%");
                  });
            });
        }

        return $query->get();
    }
}
