package com.saas.subscription.controller;

import com.saas.subscription.entity.SubscriptionPlan;
import com.saas.subscription.entity.TenantSubscription;
import com.saas.subscription.service.SubscriptionService;
import com.saas.shared.core.TenantContext;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/tenant/subscription")
@RequiredArgsConstructor
public class SubscriptionTenantController {

    private final SubscriptionService subscriptionService;

    @GetMapping("/current")
    @PreAuthorize("hasAnyRole('TENANT_ADMIN', 'TENANT_USER')")
    public ResponseEntity<TenantSubscription> getCurrentSubscription() {
        String tenantId = TenantContext.getTenantId();
        return subscriptionService.getTenantSubscription(tenantId)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @GetMapping("/plans")
    @PreAuthorize("hasRole('TENANT_ADMIN')")
    public ResponseEntity<List<SubscriptionPlan>> getAvailablePlans() {
        // Tenants can see plans to upgrade
        return ResponseEntity.ok(subscriptionService.getPublicPlans());
    }
}
