CREATE TABLE style_infos (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    company_id BIGINT UNSIGNED NOT NULL,
    buyer_id BIGINT UNSIGNED NULL,
    brand_id BIGINT UNSIGNED NULL,
    season_name VARCHAR(100) NULL,
    product_type VARCHAR(100) NULL,
    segment VARCHAR(100) NULL,
    category VARCHAR(150) NULL,
    end_user VARCHAR(100) NULL,
    risk VARCHAR(50) NULL,
    risk_type VARCHAR(150) NULL,
    style_type VARCHAR(100) NULL,
    pd_style VARCHAR(150) NULL,
    style_name VARCHAR(180) NOT NULL,
    style_no VARCHAR(80) NULL,
    is_repeat ENUM('Yes', 'No') NOT NULL DEFAULT 'No',
    team VARCHAR(120) NULL,
    status ENUM('active', 'inactive') NOT NULL DEFAULT 'active',
    created_by BIGINT UNSIGNED NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP NULL,
    KEY idx_style_info_company (company_id, deleted_at),
    KEY idx_style_info_buyer (buyer_id),
    CONSTRAINT fk_style_info_company FOREIGN KEY (company_id) REFERENCES companies (id),
    CONSTRAINT fk_style_info_buyer FOREIGN KEY (buyer_id) REFERENCES buyers (id),
    CONSTRAINT fk_style_info_brand FOREIGN KEY (brand_id) REFERENCES brands (id)
) ENGINE=InnoDB;

INSERT INTO permissions (code, name, module) VALUES
('style_info.view', 'View Style Info', 'style_info'),
('style_info.add', 'Add Style Info', 'style_info'),
('style_info.edit', 'Edit Style Info', 'style_info'),
('style_info.delete', 'Delete Style Info', 'style_info')
ON DUPLICATE KEY UPDATE name = VALUES(name), module = VALUES(module);

INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id
FROM roles r
CROSS JOIN permissions p
WHERE r.code = 'ADMIN'
  AND r.is_system = 1
  AND p.module = 'style_info';
