package org.innovateuk.ifs.organisation.transactional;

import org.innovateuk.ifs.address.resource.AddressResource;
import org.innovateuk.ifs.address.resource.OrganisationAddressType;
import org.innovateuk.ifs.commons.security.SecuredBySpring;
import org.innovateuk.ifs.commons.service.ServiceResult;
import org.innovateuk.ifs.organisation.resource.OrganisationResource;
import org.innovateuk.ifs.organisation.resource.OrganisationSearchResult;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.parameters.P;

import java.util.List;
import java.util.Set;

/**
 * Represents operations surrounding the use of Organisations in the system
 */
public interface OrganisationService {

    @PreAuthorize("hasPermission(#applicationId, 'org.innovateuk.ifs.application.resource.ApplicationResource', 'READ')")
    ServiceResult<Set<OrganisationResource>> findByApplicationId(long applicationId);

    @PostAuthorize("hasPermission(returnObject, 'READ')")
    ServiceResult<OrganisationResource> findById(long organisationId);

    @PostAuthorize("hasPermission(returnObject, 'READ')")
    ServiceResult<OrganisationResource> getByUserAndApplicationId(long userId, long applicationId);

    @PostAuthorize("hasPermission(returnObject, 'READ')")
    ServiceResult<OrganisationResource> getByUserAndProjectId(long userId, long projectId);

    @PostFilter("hasPermission(filterObject, 'READ')")
    ServiceResult<List<OrganisationResource>> getAllByUserId(long userId);

    @PostFilter("hasPermission(filterObject, 'READ')")
    ServiceResult<List<OrganisationResource>> getOrganisations(long userId, boolean international);

    @PreAuthorize("hasPermission(#organisation, 'CREATE')")
    ServiceResult<OrganisationResource> create(@P("organisation") OrganisationResource organisation);

    @PreAuthorize("hasPermission(#organisation, 'UPDATE')")
    ServiceResult<OrganisationResource> update(@P("organisation") OrganisationResource organisationResource);

    @PreAuthorize("hasPermission(#organisation, 'SYNC_COMPANIES_HOUSE_DETAILS')")
    ServiceResult<OrganisationResource> syncCompaniesHouseDetails(@P("organisation") OrganisationResource organisationResource);

    @PreAuthorize("hasPermission(#organisationId, 'org.innovateuk.ifs.organisation.resource.OrganisationResource', 'UPDATE')")
    ServiceResult<OrganisationResource> updateOrganisationNameAndRegistration(final long organisationId, final String organisationName, final String registrationNumber);

    @PreAuthorize("hasPermission(#organisationId, 'org.innovateuk.ifs.organisation.resource.OrganisationResource', 'UPDATE')")
    ServiceResult<OrganisationResource> addAddress(@P("organisationId") long organisationId, OrganisationAddressType addressType, AddressResource addressResource);

    @PostFilter("hasPermission(filterObject, 'READ')")
    ServiceResult<List<OrganisationSearchResult>> searchAcademic(String organisationName, int maxItems);

    @PostAuthorize("hasPermission(returnObject, 'READ')")
    ServiceResult<OrganisationSearchResult> getSearchOrganisation(@P("organisationId") long searchOrganisationId);

    @PreAuthorize("hasAuthority('system_maintainer')")
    @SecuredBySpring(value = "READ", description = "Only the system maintainer can search by companies house Id.")
    ServiceResult<List<OrganisationResource>> findOrganisationsByCompaniesHouseId(String companiesHouseId);

    @PreAuthorize("hasAuthority('system_maintainer')")
    @SecuredBySpring(value = "READ", description = "Only the system maintainer can search by company name.")
    ServiceResult<List<OrganisationResource>> findOrganisationsByName(String organisationName);
}

@@ -3291,426 +3291,212 @@
 %22has
-Authority('system_maintainer')%22)%0A    @SecuredBySpring(value = %22READ%22, description = %22Only the system maintainer can search by companies house Id.%22)%0A    ServiceResult%3CList%3COrganisationResource%3E%3E findOrganisationsByCompaniesHouseId(String companiesHouseId);%0A%0A    @PreAuthorize(%22hasAuthority('system_maintainer')%22)%0A    @SecuredBySpring(value = %22READ%22, description = %22Only the system maintainer can search by company name.
+Permission(#organisation, 'UPDATE')%22)%0A    ServiceResult%3CList%3COrganisationResource%3E%3E findOrganisationsByCompaniesHouseId(String companiesHouseId);%0A%0A    @PreAuthorize(%22hasPermission(#organisation, 'UPDATE')
 %22)%0A 

use these security annotations instead