SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

-- --------------------------------------------------------

--
-- Struktura tabeli dla tabeli `xyz_orders`
--

CREATE TABLE `xyz_orders` (
  `id` int NOT NULL,
  `product_name` varchar(255) NOT NULL,
  `quantity` int DEFAULT '1',
  `purchase_place` varchar(255) DEFAULT NULL,
  `price_per_unit` decimal(10,2) DEFAULT NULL,
  `delivery_date` date DEFAULT NULL,
  `notes` text,
  `status` enum('nowe','w trakcie realizacji','zrealizowane','anulowane') DEFAULT 'nowe',
  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `recipient` varchar(255) DEFAULT NULL,
  `delivery_address` text,
  `part_number` varchar(100) DEFAULT NULL,
  `company` varchar(100) DEFAULT NULL,
  `is_archived` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- --------------------------------------------------------

--
-- Struktura tabeli dla tabeli `xyz_order_comments`
-- (NOWA TABELA DLA WĄTKÓW/KOMENTARZY)
--

CREATE TABLE `xyz_order_comments` (
  `id` int NOT NULL,
  `order_id` int NOT NULL,
  `user_id` int NOT NULL,
  `comment_text` text NOT NULL,
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- --------------------------------------------------------

--
-- Struktura tabeli dla tabeli `xyz_order_history`
--

CREATE TABLE `xyz_order_history` (
  `id` int NOT NULL,
  `order_id` int NOT NULL,
  `user_id` int NOT NULL,
  `action` text NOT NULL,
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- --------------------------------------------------------

--
-- Struktura tabeli dla tabeli `xyz_settings`
--

CREATE TABLE `xyz_settings` (
  `setting_key` varchar(50) NOT NULL,
  `setting_value` text
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- --------------------------------------------------------

--
-- Struktura tabeli dla tabeli `xyz_users`
--

CREATE TABLE `xyz_users` (
  `id` int NOT NULL,
  `username` varchar(50) NOT NULL,
  `email` varchar(255) DEFAULT NULL,
  `display_name` varchar(255) DEFAULT NULL,
  `password` varchar(255) NOT NULL,
  `role` enum('admin','user') DEFAULT 'user',
  `oauth_provider` varchar(50) DEFAULT NULL,
  `oauth_subject` varchar(191) DEFAULT NULL,
  `oauth_tenant_id` varchar(64) DEFAULT NULL,
  `last_login_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- --------------------------------------------------------

--
-- Indeksy dla zrzutów tabel
--

ALTER TABLE `xyz_orders`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `xyz_order_comments`
  ADD PRIMARY KEY (`id`),
  ADD KEY `order_id` (`order_id`),
  ADD KEY `user_id` (`user_id`);

ALTER TABLE `xyz_order_history`
  ADD PRIMARY KEY (`id`),
  ADD KEY `order_id` (`order_id`);

ALTER TABLE `xyz_settings`
  ADD PRIMARY KEY (`setting_key`);

ALTER TABLE `xyz_users`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `username` (`username`),
  ADD UNIQUE KEY `uniq_users_email` (`email`),
  ADD KEY `idx_users_oauth_identity` (`oauth_provider`,`oauth_subject`,`oauth_tenant_id`);

--
-- AUTO_INCREMENT dla zrzuconych tabel
--

ALTER TABLE `xyz_orders`
  MODIFY `id` int NOT NULL AUTO_INCREMENT;

ALTER TABLE `xyz_order_comments`
  MODIFY `id` int NOT NULL AUTO_INCREMENT;

ALTER TABLE `xyz_order_history`
  MODIFY `id` int NOT NULL AUTO_INCREMENT;

ALTER TABLE `xyz_users`
  MODIFY `id` int NOT NULL AUTO_INCREMENT;

COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
