package com.saas.tenant.repository;

import com.saas.tenant.entity.PhysicalResource;
import com.saas.tenant.entity.ResourceStatus;
import com.saas.tenant.entity.ResourceType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface PhysicalResourceRepository extends JpaRepository<PhysicalResource, Long> {

    List<PhysicalResource> findByStatus(ResourceStatus status);

    List<PhysicalResource> findByType(ResourceType type);

    List<PhysicalResource> findByDepartment(String department);

    Optional<PhysicalResource> findByIdentifier(String identifier);

    @Query("SELECT r FROM PhysicalResource r WHERE r.status = 'AVAILABLE'")
    List<PhysicalResource> findAvailableResources();

    @Query("SELECT r FROM PhysicalResource r WHERE r.type = :type AND r.status = 'AVAILABLE'")
    List<PhysicalResource> findAvailableResourcesByType(@Param("type") ResourceType type);

    @Query("SELECT r FROM PhysicalResource r WHERE r.department = :department AND r.status = 'AVAILABLE'")
    List<PhysicalResource> findAvailableResourcesByDepartment(@Param("department") String department);

    @Query("SELECT r FROM PhysicalResource r WHERE r.type = :type AND r.department = :department AND r.status = 'AVAILABLE'")
    List<PhysicalResource> findAvailableResourcesByTypeAndDepartment(
            @Param("type") ResourceType type,
            @Param("department") String department);
}
