-- 004: Issuers table (issuer-service)
CREATE TABLE IF NOT EXISTS issuers (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id UUID UNIQUE REFERENCES users(id),
    company_name VARCHAR(200) NOT NULL,
    business_license VARCHAR(100),
    contact_name VARCHAR(100),
    contact_phone VARCHAR(20),
    contact_email VARCHAR(100),
    credit_rating VARCHAR(5) NOT NULL DEFAULT 'BBB' CHECK (credit_rating IN ('AAA', 'AA', 'A', 'BBB', 'BB')),
    credit_score NUMERIC(5,2) NOT NULL DEFAULT 60.00 CHECK (credit_score BETWEEN 0 AND 100),
    issuance_quota NUMERIC(15,2) NOT NULL DEFAULT 100000,
    used_quota NUMERIC(15,2) NOT NULL DEFAULT 0,
    tier VARCHAR(10) NOT NULL DEFAULT 'silver' CHECK (tier IN ('silver', 'gold', 'platinum', 'diamond')),
    status VARCHAR(20) NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'active', 'suspended', 'terminated')),
    is_first_month BOOLEAN NOT NULL DEFAULT true,
    approved_at TIMESTAMPTZ,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_issuers_user_id ON issuers(user_id);
CREATE INDEX idx_issuers_status ON issuers(status);
CREATE INDEX idx_issuers_credit_rating ON issuers(credit_rating);
CREATE INDEX idx_issuers_tier ON issuers(tier);
