"""Add invoice_templates table

Revision ID: 001_add_invoice_templates
Revises:
Create Date: 2024-01-01 00:00:00.000000

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '001_add_invoice_templates'
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    op.create_table(
        'invoice_templates',
        sa.Column('id', sa.UUID(), nullable=False),
        sa.Column('name', sa.String(255), nullable=False, unique=True),
        sa.Column('description', sa.String(500), nullable=True),
        sa.Column('is_default', sa.Boolean(), default=False),
        sa.Column('is_active', sa.Boolean(), default=True),
        sa.Column('created_at', sa.DateTime(), server_default=sa.func.now()),
        sa.Column('updated_at', sa.DateTime(), server_default=sa.func.now()),

        # Page Layout
        sa.Column('page_margin_top', sa.Integer(), default=20),
        sa.Column('page_margin_bottom', sa.Integer(), default=20),
        sa.Column('page_margin_left', sa.Integer(), default=20),
        sa.Column('page_margin_right', sa.Integer(), default=20),

        # Logo
        sa.Column('show_logo', sa.Boolean(), default=True),
        sa.Column('logo_width_mm', sa.Integer(), default=80),
        sa.Column('logo_height_mm', sa.Integer(), default=80),
        sa.Column('logo_position', sa.String(50), default='left'),

        # Company Info
        sa.Column('show_company_name', sa.Boolean(), default=True),
        sa.Column('show_company_address', sa.Boolean(), default=True),
        sa.Column('show_company_phone', sa.Boolean(), default=True),
        sa.Column('show_company_email', sa.Boolean(), default=True),
        sa.Column('show_company_website', sa.Boolean(), default=True),
        sa.Column('show_company_vat', sa.Boolean(), default=True),

        # Invoice Details
        sa.Column('show_invoice_number', sa.Boolean(), default=True),
        sa.Column('show_invoice_date', sa.Boolean(), default=True),
        sa.Column('show_due_date', sa.Boolean(), default=True),
        sa.Column('show_payment_terms', sa.Boolean(), default=True),
        sa.Column('show_branch', sa.Boolean(), default=True),
        sa.Column('show_generated_at', sa.Boolean(), default=True),

        # Customer Info
        sa.Column('show_customer', sa.Boolean(), default=True),
        sa.Column('show_customer_address', sa.Boolean(), default=True),
        sa.Column('show_customer_phone', sa.Boolean(), default=True),
        sa.Column('show_customer_email', sa.Boolean(), default=True),
        sa.Column('show_customer_vat', sa.Boolean(), default=True),

        # Items Table
        sa.Column('items_columns', postgresql.JSON(), nullable=True),
        sa.Column('items_header_background', sa.String(7), default='#343a40'),
        sa.Column('items_header_text_color', sa.String(7), default='#ffffff'),
        sa.Column('items_border_color', sa.String(7), default='#dee2e6'),
        sa.Column('items_stripe_color', sa.String(7), default='#f8f9fa'),
        sa.Column('items_font_size', sa.Integer(), default=12),

        # Totals
        sa.Column('show_totals', sa.Boolean(), default=True),
        sa.Column('show_subtotal', sa.Boolean(), default=True),
        sa.Column('show_discount', sa.Boolean(), default=True),
        sa.Column('show_vat', sa.Boolean(), default=True),
        sa.Column('show_total', sa.Boolean(), default=True),
        sa.Column('totals_background_color', sa.String(7), default='#f8f9fa'),
        sa.Column('totals_font_size', sa.Integer(), default=14),

        # Footer
        sa.Column('show_footer', sa.Boolean(), default=True),
        sa.Column('show_footer_text', sa.Boolean(), default=True),
        sa.Column('show_terms_conditions', sa.Boolean(), default=True),
        sa.Column('show_bank_details', sa.Boolean(), default=False),
        sa.Column('footer_text', sa.String(500), default='Thank you for your business!'),
        sa.Column('footer_background_color', sa.String(7), default='#ffffff'),
        sa.Column('footer_font_size', sa.Integer(), default=11),

        # Styling
        sa.Column('font_family', sa.String(50), default='Helvetica'),
        sa.Column('base_font_size', sa.Integer(), default=12),
        sa.Column('primary_color', sa.String(7), default='#007bff'),
        sa.Column('secondary_color', sa.String(7), default='#6c757d'),
        sa.Column('accent_color', sa.String(7), default='#28a745'),
        sa.Column('text_color', sa.String(7), default='#000000'),
        sa.Column('border_color', sa.String(7), default='#dee2e6'),

        # Config JSON for future extensibility
        sa.Column('config', postgresql.JSON(), nullable=True),

        sa.PrimaryKeyConstraint('id')
    )


def downgrade():
    op.drop_table('invoice_templates')
