// ==============================================================================
// Micro-SaaS - Contracts
// Interfaces, DTOs e Enums do exemplo Micro-SaaS
// ==============================================================================

namespace MicroSaaS.Contracts;

// ==============================================================================
// ENUMS
// ==============================================================================

public enum TenantStatus
{
    Active,
    Suspended,
    Deleted
}

public enum TenantUserRole
{
    Owner,
    Admin,
    Member,
    Viewer
}

public enum TenantUserStatus
{
    Pending,
    Active,
    Inactive
}

public enum SubscriptionStatus
{
    Trial,
    Active,
    PastDue,
    Canceled,
    Expired
}

public enum BillingCycle
{
    Monthly,
    Yearly
}

public enum PaymentStatus
{
    Pending,
    Confirmed,
    Received,
    Overdue,
    Refunded,
    Chargeback
}

public enum PaymentMethod
{
    Pix,
    Boleto,
    CreditCard
}

// ==============================================================================
// DTOs - TENANT
// ==============================================================================

public record CreateTenantRequest(
    string Name,
    string Slug,
    string OwnerEmail,
    string OwnerExternalId
);

public record UpdateTenantRequest(
    string Name,
    string? Description,
    string? LogoUrl
);

public record TenantResponse(
    int Id,
    string Name,
    string Slug,
    string? Description,
    string? LogoUrl,
    TenantStatus Status,
    string? CustomDomain,
    SubscriptionSummaryResponse? Subscription,
    DateTime CreatedAt
);

public record TenantSummaryResponse(
    int Id,
    string Name,
    string Slug,
    TenantStatus Status
);

// ==============================================================================
// DTOs - TENANT USER
// ==============================================================================

public record InviteUserRequest(
    string Email,
    TenantUserRole Role = TenantUserRole.Member
);

public record UpdateUserRoleRequest(
    TenantUserRole Role
);

public record TenantUserResponse(
    int Id,
    string Email,
    string? Name,
    TenantUserRole Role,
    TenantUserStatus Status,
    DateTime? LastLoginAt,
    DateTime CreatedAt
);

// ==============================================================================
// DTOs - PLAN
// ==============================================================================

public record PlanResponse(
    int Id,
    string Name,
    string? Description,
    decimal PriceMonthly,
    decimal PriceYearly,
    PlanLimits Limits,
    PlanFeatures Features,
    bool IsActive
);

public record PlanLimits(
    int MaxUsers,
    long MaxStorageBytes,
    int MaxApiRequestsPerMonth
);

public record PlanFeatures(
    bool CustomDomain,
    bool ApiAccess,
    bool PrioritySupport,
    bool AdvancedAnalytics
);

// ==============================================================================
// DTOs - SUBSCRIPTION
// ==============================================================================

public record CreateSubscriptionRequest(
    int PlanId,
    BillingCycle BillingCycle,
    PaymentMethod PaymentMethod
);

public record UpgradeSubscriptionRequest(
    int NewPlanId,
    bool ImmediateUpgrade = true
);

public record SubscriptionResponse(
    int Id,
    int TenantId,
    PlanResponse Plan,
    SubscriptionStatus Status,
    BillingCycle BillingCycle,
    DateTime CurrentPeriodStart,
    DateTime CurrentPeriodEnd,
    DateTime? TrialEndsAt,
    DateTime? CanceledAt,
    string? AsaasSubscriptionId,
    DateTime CreatedAt
);

public record SubscriptionSummaryResponse(
    int Id,
    string PlanName,
    SubscriptionStatus Status,
    DateTime CurrentPeriodEnd,
    int DaysRemaining
);

// ==============================================================================
// DTOs - PAYMENT
// ==============================================================================

public record PaymentResponse(
    int Id,
    decimal Amount,
    PaymentStatus Status,
    PaymentMethod Method,
    DateTime? PaidAt,
    DateTime DueDate,
    string? AsaasPaymentId,
    string? InvoiceUrl,
    string? PixQrCode,
    string? BoletoBarcode
);

// ==============================================================================
// DTOs - DASHBOARD
// ==============================================================================

public record DashboardResponse(
    TenantSummaryResponse Tenant,
    SubscriptionSummaryResponse? Subscription,
    DashboardMetrics Metrics,
    IReadOnlyList<RecentActivityItem> RecentActivity
);

public record DashboardMetrics(
    int TotalUsers,
    int ActiveUsers,
    long StorageUsedBytes,
    long StorageLimitBytes,
    int ApiRequestsThisMonth,
    int ApiRequestsLimit
);

public record RecentActivityItem(
    string Description,
    string? UserId,
    string? UserName,
    DateTime Timestamp
);

// ==============================================================================
// DTOs - WEBHOOKS
// ==============================================================================

public record AsaasWebhookPayload(
    string Event,
    AsaasPaymentData? Payment
);

public record AsaasPaymentData(
    string Id,
    string Customer,
    string? Subscription,
    decimal Value,
    decimal NetValue,
    string Status,
    string BillingType,
    DateTime? PaymentDate,
    DateTime DueDate
);

// ==============================================================================
// INTERFACES - SERVICES
// ==============================================================================

public interface ITenantService
{
    Task<TenantResponse> CreateAsync(CreateTenantRequest request, CancellationToken ct = default);
    Task<TenantResponse?> GetByIdAsync(int id, CancellationToken ct = default);
    Task<TenantResponse?> GetBySlugAsync(string slug, CancellationToken ct = default);
    Task<TenantResponse> UpdateAsync(int id, UpdateTenantRequest request, CancellationToken ct = default);
    Task SuspendAsync(int id, CancellationToken ct = default);
    Task ActivateAsync(int id, CancellationToken ct = default);
}

public interface ITenantUserService
{
    Task<IReadOnlyList<TenantUserResponse>> GetByTenantAsync(int tenantId, CancellationToken ct = default);
    Task<TenantUserResponse> InviteAsync(int tenantId, InviteUserRequest request, CancellationToken ct = default);
    Task<TenantUserResponse> UpdateRoleAsync(int tenantId, int userId, UpdateUserRoleRequest request, CancellationToken ct = default);
    Task RemoveAsync(int tenantId, int userId, CancellationToken ct = default);
    Task RecordLoginAsync(string externalUserId, CancellationToken ct = default);
}

public interface ISubscriptionService
{
    Task<SubscriptionResponse> CreateAsync(int tenantId, CreateSubscriptionRequest request, CancellationToken ct = default);
    Task<SubscriptionResponse?> GetCurrentAsync(int tenantId, CancellationToken ct = default);
    Task<SubscriptionResponse> UpgradeAsync(int tenantId, UpgradeSubscriptionRequest request, CancellationToken ct = default);
    Task CancelAsync(int tenantId, CancellationToken ct = default);
    Task<IReadOnlyList<PaymentResponse>> GetPaymentsAsync(int tenantId, CancellationToken ct = default);
}

public interface IPlanService
{
    Task<IReadOnlyList<PlanResponse>> GetAllAsync(CancellationToken ct = default);
    Task<PlanResponse?> GetByIdAsync(int id, CancellationToken ct = default);
}

public interface IDashboardService
{
    Task<DashboardResponse> GetDashboardAsync(int tenantId, CancellationToken ct = default);
}

public interface IWebhookService
{
    Task HandleAsaasWebhookAsync(AsaasWebhookPayload payload, CancellationToken ct = default);
}

// ==============================================================================
// INTERFACES - REPOSITORIES
// ==============================================================================

public interface ITenantRepository
{
    Task<Tenant?> GetByIdAsync(int id, CancellationToken ct = default);
    Task<Tenant?> GetBySlugAsync(string slug, CancellationToken ct = default);
    Task<bool> SlugExistsAsync(string slug, CancellationToken ct = default);
    Task AddAsync(Tenant tenant, CancellationToken ct = default);
    void Update(Tenant tenant);
}

public interface ITenantUserRepository
{
    Task<IReadOnlyList<TenantUser>> GetByTenantAsync(int tenantId, CancellationToken ct = default);
    Task<TenantUser?> GetByIdAsync(int id, CancellationToken ct = default);
    Task<TenantUser?> GetByExternalIdAsync(string externalUserId, CancellationToken ct = default);
    Task AddAsync(TenantUser user, CancellationToken ct = default);
    void Update(TenantUser user);
    void Remove(TenantUser user);
}

public interface ISubscriptionRepository
{
    Task<Subscription?> GetByIdAsync(int id, CancellationToken ct = default);
    Task<Subscription?> GetCurrentByTenantAsync(int tenantId, CancellationToken ct = default);
    Task<Subscription?> GetByAsaasIdAsync(string asaasSubscriptionId, CancellationToken ct = default);
    Task AddAsync(Subscription subscription, CancellationToken ct = default);
    void Update(Subscription subscription);
}

public interface IPlanRepository
{
    Task<IReadOnlyList<Plan>> GetAllActiveAsync(CancellationToken ct = default);
    Task<Plan?> GetByIdAsync(int id, CancellationToken ct = default);
}

public interface IPaymentRepository
{
    Task<IReadOnlyList<SubscriptionPayment>> GetBySubscriptionAsync(int subscriptionId, CancellationToken ct = default);
    Task<SubscriptionPayment?> GetByAsaasIdAsync(string asaasPaymentId, CancellationToken ct = default);
    Task AddAsync(SubscriptionPayment payment, CancellationToken ct = default);
    void Update(SubscriptionPayment payment);
}

// ==============================================================================
// PLACEHOLDER ENTITY REFERENCES (implementados em Domain)
// ==============================================================================

// Estas classes são placeholders - a implementação real está em Domain/Entities
public class Tenant { public int Id { get; set; } }
public class TenantUser { public int Id { get; set; } }
public class Subscription { public int Id { get; set; } }
public class Plan { public int Id { get; set; } }
public class SubscriptionPayment { public int Id { get; set; } }
