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

class Cliente_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('clientes')
			->result();
	}

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

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

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

	private function crear_tabla_si_no_existe()
	{
		$sql = "CREATE TABLE IF NOT EXISTS clientes (
			id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
			nombre VARCHAR(120) NOT NULL,
			cedula VARCHAR(40) NULL,
			telefono VARCHAR(40) NULL,
			correo VARCHAR(120) NULL,
			instagram VARCHAR(120) NULL,
			direccion VARCHAR(255) NULL,
			nota VARCHAR(500) NULL,
			creado_en TIMESTAMP DEFAULT CURRENT_TIMESTAMP
		) ENGINE=InnoDB DEFAULT CHARSET=utf8";

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