use rustlavel_core::Result;
use rustlavel_db::{Schema, Table};
pub const SUBSCRIPTIONS: &str = "billing_subscriptions";
pub const INVOICES: &str = "billing_invoices";
pub const REMINDERS: &str = "billing_reminders";
pub fn define_subscriptions(t: &mut Table) {
t.id();
t.string("subscription_id").unique();
t.string("customer");
t.string("customer_name");
t.string("customer_email").nullable();
t.string("customer_phone").nullable();
t.string("plan_id");
t.string("next_plan_id").nullable();
t.string("status");
t.string("channel");
t.big_integer("period_start").default_int(0);
t.big_integer("period_end").default_int(0);
t.boolean("cancel_at_period_end").default_bool(false);
t.big_integer("created_at");
t.big_integer("updated_at");
t.index(&["customer"]);
t.index(&["status", "period_end"]);
}
pub fn define_invoices(t: &mut Table) {
t.id();
t.string("invoice_id").unique();
t.string("subscription_id");
t.string("customer");
t.string("plan_id");
t.big_integer("number");
t.big_integer("amount_minor");
t.string("currency");
t.big_integer("credits");
t.big_integer("period_start").default_int(0);
t.big_integer("period_end").default_int(0);
t.string("period_key");
t.string("status");
t.string("charge_id").nullable();
t.string("channel");
t.json("instructions").nullable();
t.big_integer("due_at");
t.big_integer("paid_at").nullable();
t.big_integer("created_at");
t.unique(&["subscription_id", "period_key"]);
t.unique(&["subscription_id", "number"]);
t.index(&["status", "due_at"]);
}
pub fn define_reminders(t: &mut Table) {
t.id();
t.string("invoice_id");
t.string("kind");
t.big_integer("sent_at");
t.unique(&["invoice_id", "kind"]);
}
pub async fn create_tables(schema: &Schema<'_>) -> Result<()> {
schema.create(SUBSCRIPTIONS, define_subscriptions).await?;
schema.create(INVOICES, define_invoices).await?;
schema.create(REMINDERS, define_reminders).await
}
pub async fn drop_tables(schema: &Schema<'_>) -> Result<()> {
schema.drop(REMINDERS).await?;
schema.drop(INVOICES).await?;
schema.drop(SUBSCRIPTIONS).await
}
rustlavel_db::migration!(
CreateBillingTables,
"2026_09_17_000200_create_billing_tables",
up: |schema| { crate::schema::create_tables(schema).await },
down: |schema| { crate::schema::drop_tables(schema).await },
);