use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use uuid::Uuid;

#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct AiPlan {
    pub id: Uuid,
    pub code: String,
    pub name: String,
    pub monthly_credits: i32,
    pub daily_action_limit: i32,
    pub allowed_models: serde_json::Value,
    pub allowed_features: serde_json::Value,
    pub is_active: bool,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct UserAiSubscription {
    pub id: Uuid,
    pub user_id: Uuid,
    pub plan_id: Uuid,
    pub role_code: Option<String>,
    pub monthly_credits_total: i32,
    pub monthly_credits_used: i32,
    pub purchased_credits_total: i32,
    pub purchased_credits_used: i32,
    pub daily_actions_used: i32,
    pub current_period_start: DateTime<Utc>,
    pub current_period_end: DateTime<Utc>,
    pub status: String,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct AiFeatureCost {
    pub id: Uuid,
    pub feature_code: String,
    pub display_name: String,
    pub default_model: String,
    pub credit_cost: i32,
    pub max_input_tokens: Option<i32>,
    pub max_output_tokens: Option<i32>,
    pub is_active: bool,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct AiUsageLog {
    pub id: Uuid,
    pub user_id: Uuid,
    pub role_code: Option<String>,
    pub feature_code: String,
    pub model_alias: String,
    pub credits_charged: i32,
    pub input_tokens: Option<i32>,
    pub output_tokens: Option<i32>,
    pub total_tokens: Option<i32>,
    pub status: String,
    pub request_id: Option<String>,
    pub error_message: Option<String>,
    pub created_at: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct AiCreditTransaction {
    pub id: Uuid,
    pub user_id: Uuid,
    pub transaction_type: String,
    pub source: String,
    pub credits: i32,
    pub balance_after: i32,
    pub reference_id: Option<Uuid>,
    pub description: Option<String>,
    pub created_at: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct AiAutoApplySettings {
    pub id: Uuid,
    pub user_id: Uuid,
    pub is_enabled: bool,
    pub preferred_titles: Option<serde_json::Value>,
    pub preferred_locations: Option<serde_json::Value>,
    pub preferred_job_types: Option<serde_json::Value>,
    pub preferred_work_modes: Option<serde_json::Value>,
    pub preferred_skills: Option<serde_json::Value>,
    pub min_salary: Option<i32>,
    pub max_salary: Option<i32>,
    pub max_applications_per_day: i32,
    pub require_user_approval: bool,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct AiCreditPackage {
    pub id: Uuid,
    pub name: String,
    pub description: Option<String>,
    pub credits: i32,
    pub price_inr: i32,
    pub is_active: bool,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct AiAutoApplyLog {
    pub id: Uuid,
    pub user_id: Uuid,
    pub job_id: Uuid,
    pub match_score: Option<i32>,
    pub status: String,
    pub credits_charged: i32,
    pub generated_cover_letter: Option<String>,
    pub applied_at: Option<DateTime<Utc>>,
    pub created_at: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct AiAutoRequestSettings {
    pub id: Uuid,
    pub user_id: Uuid,
    pub professional_role_code: String,
    pub is_enabled: bool,
    pub preferred_categories: Option<serde_json::Value>,
    pub preferred_locations: Option<serde_json::Value>,
    pub preferred_requirement_types: Option<serde_json::Value>,
    pub min_budget: Option<i32>,
    pub max_budget: Option<i32>,
    pub max_requests_per_day: i32,
    pub require_user_approval: bool,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct AiAutoRequestLog {
    pub id: Uuid,
    pub user_id: Uuid,
    pub requirement_id: Uuid,
    pub professional_role_code: String,
    pub match_score: Option<i32>,
    pub status: String,
    pub credits_charged: i32,
    pub requested_at: Option<DateTime<Utc>>,
    pub created_at: DateTime<Utc>,
}
