using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace DysonNetwork.Sphere.Post; [ApiController] [Route("api/[controller]")] public class PublisherSubscriptionController( PublisherSubscriptionService subs, AppDatabase db, ILogger logger ) : ControllerBase { public class SubscriptionStatusResponse { public bool IsSubscribed { get; set; } public long PublisherId { get; set; } public string PublisherName { get; set; } = string.Empty; } public class SubscribeRequest { public int? Tier { get; set; } } /// /// Check if the current user is subscribed to a publisher /// /// Publisher ID to check /// Subscription status [HttpGet("{publisherId}/status")] [Authorize] public async Task> CheckSubscriptionStatus(long publisherId) { if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized(); // Check if the publisher exists var publisher = await db.Publishers.FindAsync(publisherId); if (publisher == null) return NotFound("Publisher not found"); var isSubscribed = await subs.SubscriptionExistsAsync(currentUser.Id, publisherId); return new SubscriptionStatusResponse { IsSubscribed = isSubscribed, PublisherId = publisherId, PublisherName = publisher.Name }; } /// /// Create or activate a subscription to a publisher /// /// Publisher ID to subscribe to /// Subscription details /// The created or activated subscription [HttpPost("{publisherId}/subscribe")] [Authorize] public async Task> Subscribe( long publisherId, [FromBody] SubscribeRequest request) { if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized(); // Check if the publisher exists var publisher = await db.Publishers.FindAsync(publisherId); if (publisher == null) return NotFound("Publisher not found"); try { var subscription = await subs.CreateSubscriptionAsync( currentUser.Id, publisherId, request.Tier ?? 0 ); return subscription; } catch (Exception ex) { logger.LogError(ex, "Error subscribing to publisher {PublisherId}", publisherId); return StatusCode(500, "Failed to create subscription"); } } /// /// Cancel a subscription to a publisher /// /// Publisher ID to unsubscribe from /// Success status [HttpPost("{publisherId}/unsubscribe")] [Authorize] public async Task Unsubscribe(long publisherId) { if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized(); // Check if the publisher exists var publisher = await db.Publishers.FindAsync(publisherId); if (publisher == null) return NotFound("Publisher not found"); var success = await subs.CancelSubscriptionAsync(currentUser.Id, publisherId); if (success) return Ok(new { message = "Subscription cancelled successfully" }); return NotFound("Active subscription not found"); } /// /// Get all subscriptions for the current user /// /// List of active subscriptions [HttpGet("current")] [Authorize] public async Task>> GetCurrentSubscriptions() { if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized(); var subscriptions = await subs.GetAccountSubscriptionsAsync(currentUser.Id); return subscriptions; } }