-- Extend clients table with tax-specific fields
ALTER TABLE clients ADD COLUMN IF NOT EXISTS business_registration_number VARCHAR(20);
ALTER TABLE clients ADD COLUMN IF NOT EXISTS business_type VARCHAR(50);
ALTER TABLE clients ADD COLUMN IF NOT EXISTS establishment_date DATE;
ALTER TABLE clients ADD COLUMN IF NOT EXISTS annual_revenue_range VARCHAR(50);
ALTER TABLE clients ADD COLUMN IF NOT EXISTS employee_count INT;
ALTER TABLE clients ADD COLUMN IF NOT EXISTS last_tax_filing_date DATE;
ALTER TABLE clients ADD COLUMN IF NOT EXISTS tax_risk_level VARCHAR(20) DEFAULT 'normal';
ALTER TABLE clients ADD COLUMN IF NOT EXISTS next_filing_due_date DATE;
ALTER TABLE clients ADD COLUMN IF NOT EXISTS contact_person VARCHAR(100);
ALTER TABLE clients ADD COLUMN IF NOT EXISTS company_id INT REFERENCES companies(id) ON DELETE SET NULL;

-- Create tax_profiles table for detailed tax information
CREATE TABLE IF NOT EXISTS tax_profiles (
    id                      SERIAL          PRIMARY KEY,
    client_id               INT             NOT NULL UNIQUE REFERENCES clients(id) ON DELETE CASCADE,
    business_registration   VARCHAR(20),
    business_type           VARCHAR(50),
    establishment_date      DATE,
    annual_revenue_range    VARCHAR(50),
    employee_count          INT,
    accounting_method       VARCHAR(50),
    fiscal_year_end         VARCHAR(10),
    last_filing_date        DATE,
    next_filing_due_date    DATE,
    tax_risk_level          VARCHAR(20) DEFAULT 'normal',
    previous_audit_history  BOOLEAN DEFAULT FALSE,
    special_notes           TEXT,
    created_at              TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at              TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Create tax_filing_schedules table for tracking schedules
CREATE TABLE IF NOT EXISTS tax_filing_schedules (
    id                      SERIAL          PRIMARY KEY,
    client_id               INT             NOT NULL REFERENCES clients(id) ON DELETE CASCADE,
    filing_type             VARCHAR(100)    NOT NULL,
    due_date                DATE            NOT NULL,
    filing_year             INT             NOT NULL,
    status                  VARCHAR(50) DEFAULT 'pending',
    assigned_to             INT             REFERENCES admin_users(id) ON DELETE SET NULL,
    completed_date          DATE,
    notes                   TEXT,
    created_at              TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at              TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Create consulting_activities table for CRM (상담 활동 추적)
CREATE TABLE IF NOT EXISTS consulting_activities (
    id                      SERIAL          PRIMARY KEY,
    client_id               INT             NOT NULL REFERENCES clients(id) ON DELETE CASCADE,
    activity_type           VARCHAR(50)     NOT NULL,
    activity_date           DATE            NOT NULL,
    activity_time           TIME,
    assigned_consultant     INT             REFERENCES admin_users(id) ON DELETE SET NULL,
    description             TEXT            NOT NULL,
    outcome                 VARCHAR(100),
    next_followup_date      DATE,
    notes                   TEXT,
    created_at              TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at              TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Create contracts table (계약 관리)
CREATE TABLE IF NOT EXISTS contracts (
    id                      SERIAL          PRIMARY KEY,
    client_id               INT             NOT NULL REFERENCES clients(id) ON DELETE CASCADE,
    contract_number         VARCHAR(50)     NOT NULL UNIQUE,
    service_type            VARCHAR(100)    NOT NULL,
    contract_date           DATE            NOT NULL,
    start_date              DATE            NOT NULL,
    end_date                DATE,
    monthly_fee             NUMERIC(10, 2),
    total_amount            NUMERIC(10, 2),
    payment_status          VARCHAR(50) DEFAULT 'pending',
    status                  VARCHAR(50) DEFAULT 'active',
    notes                   TEXT,
    created_at              TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at              TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Create revenue_tracking table (매출 추적)
CREATE TABLE IF NOT EXISTS revenue_tracking (
    id                      SERIAL          PRIMARY KEY,
    client_id               INT             NOT NULL REFERENCES clients(id) ON DELETE CASCADE,
    invoice_number          VARCHAR(50)     NOT NULL UNIQUE,
    invoice_date            DATE            NOT NULL,
    service_type            VARCHAR(100),
    amount                  NUMERIC(10, 2) NOT NULL,
    payment_status          VARCHAR(50) DEFAULT 'pending',
    payment_date            DATE,
    due_date                DATE,
    notes                   TEXT,
    created_at              TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at              TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Create indexes for performance
CREATE INDEX IF NOT EXISTS idx_clients_company ON clients(company_id);
CREATE INDEX IF NOT EXISTS idx_tax_profiles_client ON tax_profiles(client_id);
CREATE INDEX IF NOT EXISTS idx_tax_filing_schedules_client ON tax_filing_schedules(client_id);
CREATE INDEX IF NOT EXISTS idx_tax_filing_schedules_due_date ON tax_filing_schedules(due_date);
CREATE INDEX IF NOT EXISTS idx_consulting_activities_client ON consulting_activities(client_id);
CREATE INDEX IF NOT EXISTS idx_contracts_client ON contracts(client_id);
CREATE INDEX IF NOT EXISTS idx_revenue_tracking_client ON revenue_tracking(client_id);
