# Generated by Django 5.2.13 on 2026-05-05 10:44

import django.db.models.deletion
import django.utils.timezone
import store.models
from decimal import Decimal
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('catalog_app', '0024_book_stock'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='Coupon',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('code', models.CharField(db_index=True, max_length=50, unique=True)),
                ('description', models.CharField(blank=True, max_length=255)),
                ('discount_type', models.CharField(choices=[('percentage', 'Percentage (%)'), ('fixed', 'Fixed Amount (KES)')], default='percentage', max_length=20)),
                ('discount_value', models.DecimalField(decimal_places=2, help_text='Percentage (0–100) or fixed KES amount.', max_digits=8)),
                ('min_order_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), help_text='Minimum cart subtotal required to apply this coupon.', max_digits=10)),
                ('max_uses', models.PositiveIntegerField(blank=True, help_text='Leave blank for unlimited uses.', null=True)),
                ('uses_count', models.PositiveIntegerField(default=0)),
                ('valid_from', models.DateTimeField(default=django.utils.timezone.now)),
                ('expires_at', models.DateTimeField(blank=True, null=True)),
                ('is_active', models.BooleanField(default=True)),
            ],
            options={
                'ordering': ['code'],
            },
        ),
        migrations.CreateModel(
            name='DeliveryArea',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=255)),
                ('county', models.CharField(max_length=100)),
                ('delivery_fee', models.DecimalField(decimal_places=2, default=Decimal('0.00'), help_text='Flat delivery fee in KES for this area.', max_digits=8)),
                ('estimated_days', models.PositiveSmallIntegerField(default=3, help_text='Typical business days from dispatch to delivery.')),
                ('is_active', models.BooleanField(default=True)),
            ],
            options={
                'verbose_name': 'Delivery Area',
                'verbose_name_plural': 'Delivery Areas',
                'ordering': ['county', 'name'],
            },
        ),
        migrations.CreateModel(
            name='PickupLocation',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=255)),
                ('address', models.CharField(max_length=512)),
                ('county', models.CharField(max_length=100)),
                ('phone', models.CharField(blank=True, max_length=20)),
                ('opening_hours', models.CharField(blank=True, help_text='e.g. Mon–Fri 8 am – 6 pm', max_length=100)),
                ('is_active', models.BooleanField(default=True)),
            ],
            options={
                'verbose_name': 'Pickup Location',
                'verbose_name_plural': 'Pickup Locations',
                'ordering': ['county', 'name'],
            },
        ),
        migrations.CreateModel(
            name='Cart',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('session_key', models.CharField(blank=True, db_index=True, max_length=64)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('user', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='store_cart', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'verbose_name': 'Cart',
                'verbose_name_plural': 'Carts',
            },
        ),
        migrations.CreateModel(
            name='Order',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('order_reference', models.CharField(db_index=True, default=store.models._generate_order_reference, help_text='Human-readable unique reference e.g. ORD-20250505-3A7F2B', max_length=30, unique=True)),
                ('session_key', models.CharField(blank=True, db_index=True, max_length=64)),
                ('total_amount', models.DecimalField(decimal_places=2, default=Decimal('0'), help_text='Sum of all item line totals (before delivery + discount).', max_digits=12)),
                ('delivery_fee', models.DecimalField(decimal_places=2, default=Decimal('0'), max_digits=8)),
                ('discount_amount', models.DecimalField(decimal_places=2, default=Decimal('0'), max_digits=10)),
                ('final_amount', models.DecimalField(decimal_places=2, default=Decimal('0'), help_text='total_amount + delivery_fee − discount_amount  (charged via M-Pesa).', max_digits=12)),
                ('delivery_type', models.CharField(choices=[('delivery', 'Home Delivery'), ('pickup', 'Pickup')], default='delivery', max_length=20)),
                ('delivery_address', models.JSONField(blank=True, help_text='Delivery address snapshot at time of checkout.', null=True)),
                ('customer_name', models.CharField(blank=True, max_length=255)),
                ('customer_email', models.EmailField(blank=True, max_length=254)),
                ('phone_number', models.CharField(blank=True, max_length=20)),
                ('notes', models.TextField(blank=True)),
                ('status', models.CharField(choices=[('pending', 'Pending'), ('payment_initiated', 'Payment Initiated'), ('paid', 'Paid'), ('failed', 'Payment Failed'), ('cancelled', 'Cancelled'), ('processing', 'Processing'), ('shipped', 'Shipped'), ('delivered', 'Delivered')], db_index=True, default='pending', max_length=30)),
                ('checkout_request_id', models.CharField(blank=True, db_index=True, max_length=100)),
                ('merchant_request_id', models.CharField(blank=True, max_length=100)),
                ('mpesa_receipt_number', models.CharField(blank=True, db_index=True, max_length=50)),
                ('transaction_id', models.CharField(blank=True, max_length=100)),
                ('payment_confirmed_at', models.DateTimeField(blank=True, null=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('coupon', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='orders', to='store.coupon')),
                ('delivery_area', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='orders', to='store.deliveryarea')),
                ('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='store_orders', to=settings.AUTH_USER_MODEL)),
                ('pickup_location', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='orders', to='store.pickuplocation')),
            ],
            options={
                'verbose_name': 'Store Order',
                'verbose_name_plural': 'Store Orders',
                'ordering': ['-created_at'],
            },
        ),
        migrations.CreateModel(
            name='MpesaTransaction',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('checkout_request_id', models.CharField(blank=True, db_index=True, max_length=100)),
                ('merchant_request_id', models.CharField(blank=True, max_length=100)),
                ('phone_number', models.CharField(max_length=20)),
                ('amount', models.DecimalField(decimal_places=2, max_digits=10)),
                ('mpesa_receipt_number', models.CharField(blank=True, max_length=50)),
                ('transaction_date', models.CharField(blank=True, help_text='Safaricom TransactionDate string e.g. 20250505143022', max_length=20)),
                ('result_code', models.IntegerField(blank=True, null=True)),
                ('result_desc', models.TextField(blank=True)),
                ('raw_callback', models.JSONField(blank=True, null=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('order', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='mpesa_transactions', to='store.order')),
            ],
            options={
                'verbose_name': 'M-Pesa Transaction',
                'verbose_name_plural': 'M-Pesa Transactions',
                'ordering': ['-created_at'],
            },
        ),
        migrations.CreateModel(
            name='OrderItem',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('unit_price', models.DecimalField(decimal_places=2, max_digits=10)),
                ('quantity', models.PositiveIntegerField()),
                ('total_price', models.DecimalField(decimal_places=2, max_digits=12)),
                ('book_snapshot', models.JSONField(help_text='Point-in-time snapshot: {id, title, author, isbn, cover_url, price}')),
                ('book', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='store_order_items', to='catalog_app.book')),
                ('order', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='items', to='store.order')),
            ],
            options={
                'ordering': ['id'],
            },
        ),
        migrations.CreateModel(
            name='CartItem',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('quantity', models.PositiveIntegerField(default=1)),
                ('added_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('book', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='store_cart_items', to='catalog_app.book')),
                ('cart', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='items', to='store.cart')),
            ],
            options={
                'ordering': ['added_at'],
                'unique_together': {('cart', 'book')},
            },
        ),
    ]
