<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Tema_model extends CI_Model
{
	public function __construct()
	{
		parent::__construct();
		$this->crear_tabla_si_no_existe();
	}

	public function obtener_todos()
	{
		return $this->db
			->order_by('id', 'DESC')
			->get('temas_custom')
			->result();
	}

	public function obtener_por_id($id)
	{
		return $this->db->where('id', (int) $id)->limit(1)->get('temas_custom')->row();
	}

	public function crear($data)
	{
		return $this->db->insert('temas_custom', $data);
	}

	public function actualizar($id, $data)
	{
		return $this->db->where('id', (int) $id)->update('temas_custom', $data);
	}

	public function eliminar($id)
	{
		return $this->db->where('id', (int) $id)->delete('temas_custom');
	}

	private function crear_tabla_si_no_existe()
	{
		$sql = "CREATE TABLE IF NOT EXISTS temas_custom (
			id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
			nombre VARCHAR(80) NOT NULL,
			bg VARCHAR(20) NOT NULL,
			card VARCHAR(20) NOT NULL,
			text VARCHAR(20) NOT NULL,
			muted VARCHAR(20) NOT NULL,
			linea VARCHAR(20) NOT NULL,
			primary_color VARCHAR(20) NOT NULL,
			ok_bg VARCHAR(20) NOT NULL,
			ok_text VARCHAR(20) NOT NULL,
			creado_en TIMESTAMP DEFAULT CURRENT_TIMESTAMP
		) ENGINE=InnoDB DEFAULT CHARSET=utf8";

		$this->db->query($sql);
	}
}
